문제

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

도움이 되었습니까?

해결책

Please try like this:

<% @artists = Artist.where(name: "Test").first %> 

<% @concertTest = @artists.concerts %> 

Note:- where will return Active record relation array.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top