Reputation: 513
This was working just fine, not sure what broke it, but now I'm getting this error:
undefined method `Name' for nil:NilClass
I'm running Rails 3.1. I have a table called "restaurants", "lists", and "list_Items". The associations are as follows:
class Restaurant < ActiveRecord::Base
#relations
has_many :list_items
has_many :reviews
class List < ActiveRecord::Base
has_many :list_items, :dependent => :destroy
class ListItem < ActiveRecord::Base
belongs_to :restaurant
belongs_to :list
The line that's giving me the problem is in the view partial that displays my list items
<tr>
<td><%= list_item.restaurant.Name %> <%= link_to 'X', list_item, :method => :delete, :remote => true %></td>
</tr>
I think this should all work fine, but given the error at top, it's not recongizing the association such that I can get at the restaurant name. Again, this was working, I don't know why it's breaking now...
As Mu correctly pointed out (thanks Mu), the problem isn't the association, it was the presence of a list_item associated with a deleted restaurant. So, it was running up against a Nil entry.
The above is resolved and now the real question (the question I should have been asking to begin with) is how to ensure this doesn't happen. What should I change to ensure that a list_item is deleted when the associated restaurant is deleted?
Upvotes: 1
Views: 391
Reputation: 7015
class Restaurant < ActiveRecord::Base
has_many :list_items, :dependent => :destroy
has_many :reviews
...
end
And you are good to go.
Upvotes: 1