I need your help:
I have Three Models:
Phonogram has_many PhonogramInstrument:
class Phonogram < ActiveRecord::Base
#... things
has_many :phonogram_instruments, source: :filter, dependent: :destroy, inverse_of: :phonogram
has_many :instruments, source: :filter, through: :phonogram_instruments
end
PhonogramInstrument belongs_to Filter:
class PhonogramInstrument < ActiveRecord::Base
belongs_to :phonogram
belongs_to :filter
end
and Filter:
class Filter < ActiveRecord::Base
end
So, I need to fetch all phonograms that have filter_id 30 for example:
@phonograms = @phonograms.includes(:phonogram_instruments).where("phonogram_instruments.filter_id = ?", 30)
That fetches me one list with one object (exactly what I was expected):
=> [#<Phonogram id: 14, title: "Test", version_title: "", notes: nil, isrc: "BR3BD1300003", release_date: "2013-02-01", created_at: "2014-02-23 16:54:24", updated_at: "2014-08-27 03:19:08">]
...but if I take this object and show its instrument:
@phonograms.first.phonogram_instruments.inspect
=> #<ActiveRecord::Associations::CollectionProxy [#<PhonogramInstrument id: 51, phonogram_id: 14, filter_id: 30, created_at: "2014-08-27 07:01:27", updated_at: "2014-08-27 07:01:27">]>
but at this phonogram I have 7 PhonogramInstruments:
Phonogram.find(14).phonogram_instruments.inspect
=> "#<ActiveRecord::Associations::CollectionProxy [
#<PhonogramInstrument id: 25, phonogram_id: 14, filter_id: 25, created_at: \"2014-06-04 06:11:14\", updated_at: \"2014-06-04 06:11:14\">,
#<PhonogramInstrument id: 26, phonogram_id: 14, filter_id: 26, created_at: \"2014-06-04 06:11:14\", updated_at: \"2014-06-04 06:11:14\">,
#<PhonogramInstrument id: 48, phonogram_id: 14, filter_id: 49, created_at: \"2014-08-27 03:19:07\", updated_at: \"2014-08-27 03:19:07\">,
#<PhonogramInstrument id: 49, phonogram_id: 14, filter_id: 50, created_at: \"2014-08-27 03:19:08\", updated_at: \"2014-08-27 03:19:08\">,
#<PhonogramInstrument id: 50, phonogram_id: 14, filter_id: 29, created_at: \"2014-08-27 07:01:27\", updated_at: \"2014-08-27 07:01:27\">,
#<PhonogramInstrument id: 51, phonogram_id: 14, filter_id: 30, created_at: \"2014-08-27 07:01:27\", updated_at: \"2014-08-27 07:01:27\">,
#<PhonogramInstrument id: 52, phonogram_id: 14, filter_id: 31, created_at: \"2014-08-27 07:01:27\", updated_at: \"2014-08-27 07:01:27\">
]>"
Help please! :)