Topic: Calling on a second model within a controller

Hi, I'm new to rails and apologize if this is trivial; however I cannot find a solution in the book I have...

On to the problem, I would like to display a list of categories to pick one from (say in a drop down menu, but that's not the issue), the categories belong to a user and the item they are attached to holds a category id in the database.  Anyway, I wrote something like this hoping to get access to the categories, but it doesn't seem to be returning anything, and it isn't throwing any errors.

@categories = Category.find(:all, :conditions =>
                                    [ "user_id = ?", session[:id] ])

That line is within a different controller (not the category one) I have
    model :user, :category
at the top, and am sure that the session id is set.  When I try to print to_s on @categories in the view nothing comes out, so I believe nothing is being set.  Can someone shed some light on this for me?  Thanks so much.

Re: Calling on a second model within a controller

In Rails, a controller has equal access to every model, so it's not limited to whatever model you use the most in the controller. Are you certain there is a category in the database with a user_id that is equal to the session[:id] value? I would take a look in the log file and see what SQL is being generated. You can then try running that SQL directly into the command line and debugging the problem at that level.

But, there is an easier way to fetch the categories which you want. First make sure that the relationship is set up in the User and Category model:

class User < ActiveRecord::Base
  has_many :categories
end

class Category < ActiveRecord::Base
  belongs_to :user
end


Then in your controller, you can first get the User then grab the categories which that user has. Like this:

@categories = User.find(session[:id]).categories

Also, this will raise an exception if it can't find a user with that id.

Railscasts - Free Ruby on Rails Screencasts

Re: Calling on a second model within a controller

Sorry, how do you have the sql log or print?  I have checked my logs, but do not see it there.

Re: Calling on a second model within a controller

Check the log/development.log file in your rails project (assuming you are in development environment). Normally SQL commands are written to there, along with many other useful things. If you started up the server with script/server this log is usually output into that terminal window as well.

BTW, the file may be really big, so be careful.

Railscasts - Free Ruby on Rails Screencasts

Re: Calling on a second model within a controller

Ya, I thought that was the log to watch, and indeed it does hold some information, but no SQL queries.  Perhaps that means the controller simply isn't issuing any queries?

For the record, I've adopted the suggestions above and changed my User model to include a
has_many :categories
line, and my categories model to have a
belongs_to :user
Finally in a controller - not user or category - I have...
model :user, :category
@categories = User.find(session[:user_id]).categories

Thanks for all the help.