Kendall Gifford
2010-Feb-11 01:25 UTC
Parameterized ActiveRecord Associations: Any such thing?
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Erol Fornoles
2010-Feb-11 02:27 UTC
Re: Parameterized ActiveRecord Associations: Any such thing?
On Feb 11, 9:25 am, Kendall Gifford <zettab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.Named scopes are already sufficient for what you''re trying to achieve: class Sprocket named_scope :versioned, lambda { |version| {:conditions => {:version => version}} } end widget.sprockets.version(version).all(:conditions => callers_conditions) -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Kendall Gifford
2010-Feb-11 02:46 UTC
Re: Parameterized ActiveRecord Associations: Any such thing?
On Feb 10, 7:27 pm, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Named scopes are already sufficient for what you''re trying to achieve: > > class Sprocket > named_scope :versioned, lambda { |version| {:conditions => {:version => version}} } > end > > widget.sprockets.version(version).all(:conditions => callers_conditions)Cool, I didn''t know that the evaluated result (Array-like-thingy) of an association could in turn be used to access named scopes w/in the associated class. Works great. 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Feb-11 14:16 UTC
Re: Parameterized ActiveRecord Associations: Any such thing?
On Feb 11, 2:46 am, Kendall Gifford <zettab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 10, 7:27 pm, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Cool, I didn''t know that the evaluated result (Array-like-thingy) of > an association could in turn be used to access named scopes w/in the > associated class. Works great. >foo.sprockets is a proxy object, which in some cases will just behave like a named_scope. Proxy extensions have been around for longer and can also be useful too, but they''re not needed as often these days. Proxy extensions are things like has_many :sprockets do def version(version) find(:all, :conditions => {:version =>version) end end In this case you gain nothing over using the named scope (in fact the named scope is more useful because you don''t have to be going through the association), but proxy extensions have access to the owner object of the association (proxy_owner), the reflection object for the association etc. which can be useful for some purposes Fred Fred -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Apparently Analagous Threads
- Scaffold generator: create vs. new
- Error : bundle install
- cache-busting non-digest assets in sprockets in development a good idea? should headers in sprockets be configurable?
- Sprockets, JST, Eco and escaping
- url helper in model.js.erb.cofee (Sprockets::Context)