Forum: Ruby on Rails problem with has_many through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4b87f676cb6c4d648d71000681823693?d=identicon&s=25 Jean-sébastien Jney (jney)
on 2007-05-04 00:48
(Received via mailing list)
another active record question :
i've got 3 classes :

1/
class Store < ActiveRecord::Base
  has_many   :store_entries
  has_many   :store_shipping_countries
end

2/
class StoreShippingCountry < ActiveRecord::Base
  belongs_to :store
  has_many   :store_entries,
    :through => :store
end

3/
class StoreEntry < ActiveRecord::Base
  belongs_to :store
  has_many   :store_shipping_countries,
    :through => :store
end

in store entry i'm calling store_shipping_countries and here is what
i've got in the log file :

store_shipping_countries.* FROM store_shipping_countries INNER JOIN
stores ON store_shipping_countries.store_id = stores.id WHERE
((stores.store_id = 1004))

i don't understand how it build the WHERE clause "((stores.store_id =
1004))". it should be (store_entries.id = 1004) !

please help.
5b9ee1ffcaf5ce4c4299d18d5f110044?d=identicon&s=25 Paul Mylchreest (Guest)
on 2007-05-04 01:10
(Received via mailing list)
Hi Jean-Sébastien,

Aren't you trying to do this instead?

1/
class Store < ActiveRecord::Base
   has_many   :store_shipping_countries
   has_many   :store_entries, :through => :store_shipping_countries
end

2/
class StoreShippingCountry < ActiveRecord::Base
   belongs_to :store
   has_many   :store_entries
end

3/
class StoreEntry < ActiveRecord::Base
   belongs_to :StoreShippingCountry
end
This topic is locked and can not be replied to.