I want a page where I display the company_name for the user. So far, I
managed to get the user_id from the session…
<%= session[:user_id] %>
Now, I guess I can use the user_id, to find it’s account_id, and then
match the account_id and get the company_name.
If you have a user in current_user for example then the company is
current_user.company, so the name is current_user.company.name. Such
is the magic of Rails. Watch out for the cas when the user does not
have a company, in which current_user.company will be nil. Check out
the rails guide on ActiveRecord relationships to see how this all
works.
OK, but did you take heed of my warning in my first reply? Can you
guarantee that a user will always have a company (even in unusual
circumstances)? Add into your automated tests one where the user does
not have a company and see what happens. (Hint, find returns nil if
it cannot find what you are asking for).
Another point, it might be worth providing a method somewhere called
current_user (possibly in application_controller) that does the find,
then you will not need to keep typing the find everywhere you want
current_user.
If you have a user in current_user for example then the company is
current_user.company, so the name is current_user.company.name. Such
is the magic of Rails. Watch out for the cas when the user does not
have a company, in which current_user.company will be nil. Check out
the rails guide on ActiveRecord relationships to see how this all
works.
OK, but did you take heed of my warning in my first reply? Can you
guarantee that a user will always have a company (even in unusual
circumstances)? Add into your automated tests one where the user does
not have a company and see what happens. (Hint, find returns nil if
it cannot find what you are asking for).
Every time an user is created, the account_id is recorded on its row
too. But sure it’s good to have a safety net.
Another point, it might be worth providing a method somewhere called
current_user (possibly in application_controller) that does the find,
then you will not need to keep typing the find everywhere you want
current_user.
Another point, it might be worth providing a method somewhere called
current_user (possibly in application_controller) that does the find,
then you will not need to keep typing the find everywhere you want
current_user.
I’m trying to do exactly that, but the app doesn’t seem to find the
session if placed on the application controller.
DEPRECATION WARNING: Disabling sessions for a single controller has been
deprecated. Sessions are now lazy loaded. So if you don’t access them,
consider them off. You can still modify the session cookie options with
request.session_options. (called from
[…]/app/controllers/application_controller.rb:6)
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.
Doing it like that means that it will get executed when the controller
loads, which in production will be only once. You need to put it in a
method of application_controller then call it from your code when you
want to know the result.
Are you using authlogic? If so I am sure the examples of how to use
it include how to do a current_user method.
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.
Thanks, ok, so I did this…
APPLICATION CONTROLLER
class ApplicationController < ActionController::Base
before_filter :authorize
Doing it like that means that it will get executed when the controller
loads, which in production will be only once. You need to put it in a
method of application_controller then call it from your code when you
want to know the result.
Are you using authlogic? If so I am sure the examples of how to use
it include how to do a current_user method.
So you mean doing something like this in the application controller?
def current_user
User.find_by_id(session[:user_id])
end
I use the user id, the account id, the account name and the account id
in several parts of the application. Everytime I need to access the data
I have to add something like this on each controller method…
So you mean doing something like this in the application controller?
def current_user
User.find_by_id(session[:user_id])
end
That’s usually sensible.
Since current_user is defined in
ApplicationController, and since all your other controllers inherit from
ApplicationController, all you need in your controller is
@current_user = current_user
and then do @current_user.account.whatever in the view.
How can I make the company name available to all views with something
like this… @company_name
I tried it, and it does work. But how can I make it work on the
APPLICATION CONTROLLER instead of on an individual controller. Several
views need the account_id, so I would have to repeat myself in several
controllers writing @company_name = company_name
For example with this code in the application controller…
def company_id @company_id = User.find_by_id(session[:user_id]).account.id
end @company_id = company_id
I get this error…
Routing Error
undefined local variable or method `company_id’ for
ApplicationController:Class
ERROR
Routing Error
undefined local variable or method `company_id’ for
ApplicationController:Class
Why would it say the method is undefined, if it ISSSS defined? and why
would it have something to do with the routes if it’s just a simple
method and a simple variable?
So you mean doing something like this in the application controller?
def current_user
User.find_by_id(session[:user_id])
end
That’s usually sensible.
I use the user id, the account id, the account name and the account id
in several parts of the application. Everytime I need to access the data
I have to add something like this on each controller method…
No, you certainly don’t. Since current_user is defined in
ApplicationController, and since all your other controllers inherit from
ApplicationController, all you need in your controller is
@current_user = current_user
and then do @current_user.account.whatever in the view.
How can I make the company name available to all views with something
like this… @company_name
Use a before_filter or the technique I just described.