Reputation: 101
I'm struggling to get an association to work properly in Rails. I'm just learning the language, so any help is really appreciated.
I have:
class Feedback < ActiveRecord::Base
belongs_to :section
end
class Section < ActiveRecord::Base
has_many :questions
has_many :feedbacks
end
My 'feedbacks' table has a 'section_id' column.
Then in my /show.html.erb in sections I have:
<% @section.feedback.each do |feedback| %>
<%= feedback.name %>
<% end %>
And I get the following error:
undefined method `feedback' for #<Section:0x007fa70dd2dc58>
Did you mean? feedbacks
feedbacks=
What have I done wrong?
Upvotes: 0
Views: 220
Reputation: 1943
Did you try making the feedbacks
plural?
<% @section.feedbacks.each do |feedback| %>
Here's the link to has_many
association.
Note that you pluralized it properly in the model definition.
Upvotes: 2
Reputation: 21
Since you are working with a has_many
association, when referring to the associated class, you should always use the plural tense. In this case, feedbacks
instead of feedback
. Note that this is only true for this direction. If you were to try and find a feedback's section you would keep it singular: @feedback.section
.
Upvotes: 2