Has_one :through

I was trying to find the best way around that has_one :through doesn’t
exist when I realized that, at least for some cases, it does. It’s just
called delegate.

class Owner < ActiveRecord::Base
has_many :books
has_many :pages, :through => :books
end

class Book < ActiveRecord::Base
belongs_to :owner
has_many :pages
end

class Page < ActiveRecord::Base
belongs_to :book
delegate :owner, :to => :book
end

This way you can get owner.pages and page.owner.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs