I like what you've done with fixing conditions on the regular find. But, I still like being able to find similar with existing model objects and etc.
So, I've extended your code with what I was doing in my code (aka, Razor) but used the really solid foundation you wrote with slice. I think putting our code together like this makes a much more solid plugin.
diff: http://unspace.ca/sliceandrazor.diff
zip: http://unspace.ca/slicerazor.zip
Both of our specifications from the previous emails still exist, but with the added avantage of being able to do this with AR objects.
@user.find_matches_on(:last_name, :created_on => :less_than)
My original use would not allow for the specification of predicates. But, using your backend, it was simple (flatten that trailing options hash into :created_on_less_than).
Your way of finding via attribute conditions is the same:
User.find(:all, :conditions => { :last_name => "Catlin", :created_on_less_than => Time.now} ) #yours
Though, I added this "shortcut".
User.find_with(params[:user])
I think it makes really, really nice semantic sense to have this. If someone wants a more full fledged find with limits and offsets, they are free to be more verbose. But, I think find_with(attrib) is a nice addition to ActiveRecord in the style of find_by_*.
Thoughts? Comments? Flames?
-hampton.
On 5/14/06, Tom Ward <[EMAIL PROTECTED]> wrote:
On 5/13/06, Hampton <[EMAIL PROTECTED]> wrote:
> 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)
I've been exploring similar areas with my 'slice and dice' plugin
(svn://rubyforge.org//var/svn/popdog/slice_and_dice/tags/REL-0.1). It
alters sanitize_sql to allow conditions passed in a hash (in addition
to strings and arrays). i.e:
Student.find :all, :conditions => ["last_name = ?", "Catlin"]
can be written as:
Student.find :all, :conditions => {:first_name => "Catlin"}
This condition hash (hopefully) works not just in finder methods, but
also calculations, etc. It also allows slightly more complex clauses:
Book.find :all, :conditions => {:price_more_than => 50}
Book.count :conditions => {:title_starts_with => "Zen and the art of"}
It'd be great if you released your code as a plugin. I'd like to see
how you've gone about implementing it, and anything I can learn/steal
for my own. If you've any comments on what I've done, I'd be glad to
hear them too.
Tom
--
email : tom at popdog.net
_______________________________________________
Rails-core mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-core
_______________________________________________ Rails-core mailing list [email protected] http://lists.rubyonrails.org/mailman/listinfo/rails-core
