Domanda

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

È stato utile?

Soluzione

Please try like this:

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

<% @concertTest = @artists.concerts %> 

Note:- where will return Active record relation array.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top