I wanted to hit the sonar button and see what the general reaction is to this plugin I've created.

Basically the problem it solves is filtering with ActiveRecord.

Lets say we have a large set of student records. And, we want to be able to quickly filter down to students with the last name "Smith". Currently, we'd write this.

Student.find_by_last_name(params[:student][:last_name])

-or-

Student.find(:all, :conditions => [ "last_name = ?", params[:student][:last_name] ])

Doing any sort of reusable filtering is a bit peevy. For instance, we might have a certain student, and want to find if this student has any people with his/her same first name: Student.find(:all, :conditions => [ "last_name = ?", @student.last_name]) and if I want to find with same first and several other attributes, my find becomes more complex. So, you always end up writing some semi-ugly looking code to build the condition string.

Wouldn't it be nice if ActiveRecord could make this a bit easier.

For instance,

@student.find_matches(:on => [ :last_name, :city ])

Viola, it builds a find to get matches. Only explicit matches, you can also use a similar thing for finding with forms.

Let's say you have params[:student] = { :last_name = "Catlin" }. And, anything could have been passed in. Maybe a bunch of different attributes, and we'd like to filter based on that.

Student.find_with (params[:student])

...which really works as...

Student.find_with(:last_name = "Catlin)

Do you guys think this is useful? Its already written and I've been using it on a few projects. I've personally found it useful, but is this the kind of thing that you guys think AR might need? Thoughts/comments/flames welcome.

-hampton.
_______________________________________________
Rails-core mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to