Reputation: 904
I keep getting this error
undefined method `concerts' for ActiveRecord::Relation::ActiveRecord_Relation_Artist:0xb50b691c>
When trying to do this:
<% @artists = Artist.where(name: "Test") %>
<% @concertTest = @artists.concerts %> #this line raises the error
Here are my models:
class Concert < ActiveRecord::Base
validates_presence_of :venue
validates_presence_of :date
has_many :reviews
belongs_to :artist
end
class Artist < ActiveRecord::Base
validates_presence_of :name
has_many :concerts
end
I can't seem to figure out what is causing this error, and why I can't reference the concerts of a particular artist this way. Any suggestions? Thanks
Upvotes: 0
Views: 98
Reputation: 1714
Please try like this:
<% @artists = Artist.where(name: "Test").first %>
<% @concertTest = @artists.concerts %>
Note:- where
will return Active record relation
array.
Upvotes: 1
Reputation: 904
It works when I do .find_by_name instead of .where
<% @artists = Artist.find_by_name("Test") %>
<% @concertTest = @artists.concerts %>
I'm unsure for the reason of this but maybe someone can comment to clarify it.
Upvotes: 0