views:

289

answers:

3

I'm not sure how to search for this answer, so I'll go ahead and ask it.

In my rails project I have a User model and a foo model. A user can have one or more foo models assigned to it. I have accomplished this by adding

has_many :foo, :through => :user_foo

in my user model.

Now, over in my view, I want to display a list of all foos. Not just the ones that are selected (i will be making these radio buttons, but that's another question). When I try to do this (yes, i'm using haml):

    - for foo in @foos

I get this error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

My assumption is that this is caused because the @foos collection is empty. What is the proper way to get access to this collection within my user view?

** edit **

I think my initial question was a bit confusing. the first issue i'm trying to figure out is how to access a collection of foos from within my user view. the relationship doesn't matter. I just want a list of all foos in the system. not just the ones assigned to the user.

+2  A: 

I assume that you have belongs_to :user in your Foo class?

What does your controller code look like? To show all foos it should have something like this:

def index
  @foos = Foo.all
end
John Topley
I did not have that in my foo model. Now that I've added it, I get this error:Could not find the association :user_foo in model UserIn my foo model, should it be belongs_to :user_foo then in the user_foo should it say belongs_to :foo ? that doesn't seem right either
levi rosol
ok, so i figured out that i was also missing has_many :foo in my user model. so now i have: has_many :user_foo has_many :foos, :through => :user_fooI also needed to add belongs_to :userbelongs_to :fooin my userfoo model. so this solves my xref question.
levi rosol
but my current issue is that i cannot loop through all foos in the system from the user view. so i guess disregard the whole x-ref thing and look at it like i just want to loop through all foos in the system within my user view.
levi rosol
Please post the complete User and Foo model code and your controller and view code.
John Topley
i found my issue with your help. I needed to setup the has_many and belongs as described above, and i added a foos method in my user model. but the missing items was, I needed to inherit from BaseController instead of ApplicationController for my app.
levi rosol
It still sounds like there's something weird going on with your app. Your controller classes should inherit from ApplicationController and you shouldn't need to add a foos method to your User model.
John Topley
you're correct. the BaseController thing is due to me using CommunityEngine for my site. and i was also able to remove the foos method as well after realizing i had mis-named my controller.
levi rosol
A: 

The error suggests you foo object is empty.

If you want all Foo's connected with the current user object, you use

my_user = User.find(1) # finds user with id no. 1
list_of_foos = my_user.foos # finds all foos associated with my_user

should work

If you are looking for all foo's, no matter what their association, you use

list_of_foos = Foo.find(:all)

This might not be up to the Rails syntax that's currently in fashion: it's been a while since I actively did Rails development, but this if I understand the question right, this should help you. Good luck.

wzzrd
+2  A: 

To access all Foos just use

@foos = Foo.all

in your controller.

The error you were experiencing before, the nil object error could be prevented by a check like:

- if @foos.empty?
  %p There are no Foos
- else
  ...

Also, the best way to iterate over a collection is by using the #each method, not a for loop. For example:

- @foos.each do |foo|
  %p= foo.name

So a finished example would be:

- if @foos.empty?
  %p There are no Foos
- else
  - @foos.each do |foo|
    %p= foo.name
Chris Lloyd