Mi chiedo fino a che punto posso utilizzare le associazioni in Rails. Prendere in considerazione quanto segue:rails has_many: through has_many: through
class User < ActiveRecord::Base
has_one :provider
has_many :businesses, :through => :provider
end
class Provider < ActiveRecord::Base
has_many :businesses
has_many :bids, :through => :businesses
belongs_to :user
end
class Business < ActiveRecord::Base
has_many :bids
belongs_to :provider
end
class Bid < ActiveRecord::Base
belongs_to :business
end
Sono in grado di impostare queste scorciatoie nifty come User.businesses
e Provider.bids
ma per quanto riguarda fare qualcosa di simile User.bids
? È possibile associare un'associazione, per così dire?
È possibile, ma è necessario fare attenzione a quanto profondamente si nidifica, perché è possibile impantanarsi l'app del database e delle guide. Detto questo, ho scritto un post sul blog che spiega come usare nested_has_many_through per fare questo: http://kconrails.com/2010/01/28/nesting-has_many-through-relationships-in-ruby-on-rails/ –