Dynamic scopes extension

Currently dynamic scopes in Rails only allow to scope on attributes, which is already a great feature.

However, I have made a plugin (http://github.com/jeroenvandijk/ find_by_params/tree/master) that makes this all even better and I was wondering whether I can integrate this into to the Rails core. This plugin does also create dynamic scopes for associations.

Say you have the following:

class User < ActiveRecord::Base   belongs_to :site   has_many :memberships

  named_scope :scoped_by_site, lambda { |field|, {:conditions=> ["sites.name IN (?)", field], :include=>[:sites]} }   named_scope :scoped_by_membership, lambda { |field|, {:conditions=> ["memberships.name IN (?)", field], :include=>[:sites]} } end

With my plugin the two named scopes are generated by method_missing like in the implementation of the current dynamic scopes.

The usefulness of these dynamic scopes becomes clear when you look at the three class methods my plugin adds to ActiveRecord::Base. In my controllers I always call #find_by_params with the params given. This way I have created a microformat for search forms.

E.g. The following search logic: User.find_by_params( :site => { :name => "foo" } )

with the plugin would equal to: User.find(:all, :conditions => ["sites.name IN (?)", "foo"], :include=> [:sites])

can thus be defined in the view with the following: <form action="/users" method="get" > <input type="text" name="user[site][name]"/> </form>

If you see the advantages of this you might also want to consider the following

<form action="/users" method="get" > <input type="text" name="user[site][*name*]"/> </form>

Which would trigger the dynamic scopes to use LIKE and wild cards.

I have used the above functionality in a couple of applications now and it basically removes 90% of the search code, 90% of the named_scopes. And where it does not apply directly you can override find_by_params to, implement the missing behaviour and let a 'super' call do the rest.

I see enough reasons to implement something similar in the Rails core, however with Rails 3.0 coming up and the issues with count (https:// rails.lighthouseapp.com/projects/8994/tickets/2189-count-breaks-sqlite- has_many-through-association-collection-with-named-scope) this might be a difficult issue.

Please tell me what you guys think of something like the above and if this is something for the core.