:has_many through with owner-dependent conditions


#1

I have a situation where I abstractly have a has_many through
relationship between three tables. Without the through, I’d have no
trouble passing the necessary value to the third table, but with it, I
have no access to the original object id.

This is what I’ve come up with but I’m wondering if there’s not
something better (particularly since sql_conditions isn’t really part
of the public API.)

belongs_to :property_type

has_many :properties,
:through => :property_type do
def sql_conditions
conditions = “properties.integer_value = #{@owner.id}”
if base = super
conditions = “(” + [ conditions, base ].join(")AND(") + “)”
end
conditions
end
end