Fernando Aureliano
Fernando Aureliano

Reputation: 914

Rail4 - How To Multi-level Associations?

I want to get the values in:

New -> Type -> Version

I Have this setup:

class New < ActiveRecord::Base
   has_one :type
end

class Type < ActiveRecord::Base
   belongs_to :new
   has_many :versions
end

class Version < ActiveRecord::Base
   belongs_to :type
end

How can I access @new.type.version.first ?

Thanks!

Upvotes: 1

Views: 118

Answers (1)

Brent Eicher
Brent Eicher

Reputation: 1050

Try:

class New < ActiveRecord::Base
  has_many :versions, through: :type
  ...
end

@new.versions.first

Upvotes: 2

Related Questions