2010/10/14 Leonel *.* <li...@ruby-forum.com>

> Ok, so I made this change: I used find_by_id instead of just find.
>
> APPLICATION CONTROLLER
> @company_id = User.find_by_id(session[:user_id]).account.id
>
> ERROR
> Routing Error
> undefined method `account' for nil:NilClass
>
> I'm not good at routing, but will read about it right now. If you got a
> suggestion is very much welcomed.
>
>
You might be searching for a user before a session is established, as such
the session[:user_id] is nil. You may need to revise the position of this
statement: @company_id = User.find_by_id(session[:user_id]).account.id in
your application_controller. Remember that often the application controller
the first to load even when you are just open your application. You may need
to add a condition. Something like:
@company_id = User.find_by_id(session[:user_id]).account.id if
session[:user_id]

A General Note:

Personally, I would not recommend writing
User.find_by_id(session[:user_id]).account.id although Rails natually allows
for this short cut. For testing sake (or whether you call it bug hunt), I
would find the user first and search for the user's account. In that way I
will be able to test for the avaliabilty of the user and the availability of
the account separately. I should say, despite that "every time an user is
created, the account_id is recorded on its row too", bugs are are bugs, they
always bug. Anything can happen midway. So consider revising that statement.
This is a personal preference, based on my experience. It is subject to
comments and criticisms.

Happy coding.

---
Edmond
Software Developer | Baobab Health Trust (http://www.baobabhealth.org/) |
Malawi

Cell:  +265 999 465 137 | +265 881 234 717

*"Leading the Improvement of Health through Information and Communication
Technology in the Developing World" The **Creed* of *Baobab Health
*

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to