This project is archived and is in readonly mode.
Has_many :through _ids not passing conditions to SQL
-
Chris Your
I took a look at ActiveRecord-3.0.6 in associations.rb
redefine_method("#{reflection.name.to_s.singularize}_ids")on line 1492. As far as I can see, the _ids method on thehas_many :throughassociation creates the finder on the through model like this (around line 1500:send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").map! { |r| r.send(primary_key) }I can easily add an if condition that looks for the
:conditionsoption on the reflection and add thewhereconditions like this:if reflection.options[:conditions] send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").where(reflection.options[:conditions]).map! { |r| r.send(primary_key) } else send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").map! { |r| r.send(primary_key) } endBut I'm sure there's a Rails way of doing this kind of thing.
Any help to get this patched would be appreciated.
Thanks,
Chris -
Anatoliy Lysenko
I think you should look at master not v3.0.6 if you want to create a pathc.
CollectionAssociation#reader_ids:column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}" scoped.select(column).except(:includes).map! do |record| record.send(reflection.association_primary_key) endMaybe scoped already has conditions?
-
Chris Your
Hi Anatoliy,
Thanks for the advice. I haven't contributed to Rails' source before...
Scoped on the
throughobject's reflection doesn't have thewhereconditions - the conditions are on thereflectionobject's reflection. If that makes sense.I wish I knew more about reflections and Arel. I'm learning as I dig through this source though. :)
-
Chris Your
I took a look at Rails Master. Looks awesome. Lots of changes.
I'll have to checkout master to see if the CollectionAssociation#reader_ids fixes this little bit of missing functionality in 3.0.6.