ruby on rails - How to create a scope for an intermediate table? -
i have 2 tables, :photos
, :albums
, , matching table between 2 called :photo_listings
.
i want show albums have photos inside, so, created scope in albums model:
scope :with_photos, -> {includes(:photos)}
the problem :photos
not straight column :albums
, because photos , albums matched through :photo_listings
. so, how can create scope albums lists albums assigned photos?
photo_listings model
class photolisting < activerecord::base belongs_to :album belongs_to :photo end
album model
class album < activerecord::base has_many :photo_listings end
photo model
has_many :photo_listings has_many :albums, :through => :photo_listings end
you can add together album
class album < activerecord::base has_many :photo_listings has_many :photos, through: :photo_listings # line end
then scope should work. missed photos
relation on album
model.
ruby-on-rails scope models
No comments:
Post a Comment