bits about life, coding and stuff
Just had the problem, that I wanted a has_many association in my model with a condition from a value of the same model. Turns out this is quite hard.
I know there are named_scopes, but a has_many is needed if you want to use rails’ great query methods.
You see, this condition is parsed at runtime (because we use single quotes) – and reads the current model version (lock_version) via a self.send.
It’s working, but I don’t really like this syntax. Are there any better solutions for my problem?
Anyway, here’s the source: http://alexle.net/archives/235
Related posts:
1 Response to Rails has_many and dynamic conditions
James West
July 3rd, 2011 at 7:37 pm
Rails 3 now suggests use of proc for this
e.g.
has_many :tc_links, :conditions => proc { “#{TcLink.table_name}.tc_version = #{self.send(:lock_version)}” }
See here for more info