ruby - Rails : Find with deep Association -
i have models :
ad.rb
class ad < activerecord::base belongs_to :seller, polymorphic: true scope :published, -> { where(state: "published") } end car.rb
class car < ad end dealership.rb
class dealership < activerecord::base belongs_to :agent has_many :cars, as: :seller end agent.rb
class agent < user has_one :dealership, dependent: :destroy end user.rb
class user < activerecord::base has_many :ads, as: :seller has_many :subscriptions def has_valid_subscription? !subscription.valid(self.id).blank? end end i know there's lot of polymorphism... try keep databases simple possible. clean model focus on problem.
i try ads has seller user valid subscription !
but there 2 levels of association between model... , can't !
can show me way ?
thank lot.
add seller_user association ad model
class ad < activerecord::base belongs_to :seller, polymorphic: true belongs_to :seller_user, :foreign_key => :seller_id, :class_name => "user", conditions: { ads: { seller_type: "user"}} scope :published, -> { where(state: "published") } end now can do
ad.all.collect{|ad| ad.seller_user && ad.seller_user.has_valid_subscripion?} if need ads of user subscription, can do
ad.joins(:seller_user => :subscriptions)
Comments
Post a Comment