Hi list, how are ya?
So, my current project is just begging for the ability to have
parameterized associations in my ActiveRecord classes. Basically I
need something that is a cross between named scopes and standard
associations (has_many specifically).
I'm wondering if such a thing exists or, if not, if anyone else has an
elegant, equivalent solution?
Example:
class Sprocket < ActiveRecord::Base
belongs_to :widget
belongs_to :version
end
class Widget < ActiveRecord::Base
has_many :sprockets
has_many :versioned_sprockets, lambda do |v|
:conditions => { :version => v }
end
end
The idea behind the example is so I can do stuff like:
def get_just_the_sprockets_i_need(callers_conditions)
version = Version.find(magic_method_returning_just_the_right_id)
widget = some_other_magic_method_returning_a_widget_instance
widget.versioned_sprockets(version).all(:conditions =>
callers_conditions)
end
I first considered just putting a named scope directly in the Sprocket
class, giving me parameterized scoping. However, I specifically want
to access my sprockets through a Widget, getting the implicit
widget_id scoping that comes with the association. If worse comes to
worse, I'll have an uglier named scope taking two parameters (does
this work) instead of one:
class Sprocket ...
named_scope :versioned, lambda do |widget, version|
:conditions => { :widget => widget, :version => version }
end
...
end
Any ideas?
Thanks.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.