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 Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
It sounds like it could be useful. I''d say point us towards a plugin. Kev On 5/13/06, Hampton <hcatlin@gmail.com> wrote:> 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 > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > >
On 5/13/06, Hampton <hcatlin@gmail.com> 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 Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Tom, 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 <tom@popdog.net> wrote:> > On 5/13/06, Hampton <hcatlin@gmail.com> 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 > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > >_______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
> 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 don''t think its worth adding new syntax to core to go from User.find(:all, :conditions => params[:user]) to User.find_with(params[:user]). The original method allows for a choice between :first and :all as well as all the other options like :limit. The sweetness of syntactic sugar is measured in saving x frequency. I think that neither the saving nor the frequency of the query is enough to warrant a new method. I would look sympathetic to a patch that allowed for hash definitions of :conditions, though. But I don''t buy :created_on_less_than or other ways of dealing with something else than equality through naming conventions. If you need that kind of thing, use the strings. -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework
Introducing Patch #5143! http://dev.rubyonrails.org/ticket/5143 This patch allows for a hash to be passed into the :condition parameter of the ActiveRecord::Base#find method. The functionality is described in detail in the patch and in the documentation that is with it. If anyone can run the unit tests with the patch to see if it functions correctly with Oracle and Postgres, it would be much appreciated! -hampton. I would look sympathetic to a patch that allowed for hash definitions> of :conditions, though._______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Is there anything I can do to help this patch get vetted quickly and efficiently? -hampton. On 5/21/06, Hampton <hcatlin@gmail.com> wrote:> > Introducing Patch #5143! > > http://dev.rubyonrails.org/ticket/5143 > > This patch allows for a hash to be passed into the :condition parameter of > the ActiveRecord::Base#find method. The functionality is described in detail > in the patch and in the documentation that is with it. > > If anyone can run the unit tests with the patch to see if it functions > correctly with Oracle and Postgres, it would be much appreciated! > > -hampton. > > > > I would look sympathetic to a patch that allowed for hash definitions > > of :conditions, though. > > > > >_______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
On 5/23/06, Hampton <hcatlin@gmail.com> wrote:> Is there anything I can do to help this patch get vetted quickly and > efficiently? > > -hampton.Make a plugin and get folks to use it. I''m not sold on this idea at all, personally. -- Rick Olson http://techno-weenie.net
On 5/24/06, Rick Olson <technoweenie@gmail.com> wrote:> On 5/23/06, Hampton <hcatlin@gmail.com> wrote: > > Is there anything I can do to help this patch get vetted quickly and > > efficiently? > > > > -hampton. > > Make a plugin and get folks to use it. I'm not sold on this idea at > all, personally.Here are three formats specifying the same query: Animal.find_all_by_genus "canis" Animal.find :all, :conditions => ["genus = ?", "canis"] Animal.find :all, :conditions => {:genus => "canis"} For me, the big advantage the last has is that the conditions are abstracted away from SQL, and in a format that's easy to manipulate. That said, I've got no strong desire to see it in rails core; I can get this functionality through my plugin easily enough. What would be nice though (for this and other plugins) would be some refactoring of parts of rails to make it easier to write plugins. If 'make a plugin' is going to be the default response to patches, then making plugin creation easier has got to be a worthwhile goal. Tom _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
> For me, the big advantage the last has is that the conditions are > abstracted away from SQL, and in a format that''s easy to manipulate. > That said, I''ve got no strong desire to see it in rails core; I can > get this functionality through my plugin easily enough.I''m sold on the part that just gives you and easy option of turning hashes into AND structures. So that''ll go in core.> What would be nice though (for this and other plugins) would be some > refactoring of parts of rails to make it easier to write plugins. If > ''make a plugin'' is going to be the default response to patches, then > making plugin creation easier has got to be a worthwhile goal.What parts in particular would you like to see refactored? -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework