wondering if there is a way to do this.. say we have a model that has attributes length and width.. :length and :width and we have a class square: class Square < ActiveRecord::Base def area area = length * width end end is there a way i can do a: Square.find(:area => ''144'') to produce any squares with an area of 144? i know there are lots of other ways to do this, but in the case i am working on, a find would be REALLY clean.. -- Posted via http://www.ruby-forum.com/. -- 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.
On Apr 28, 2:34 pm, Sergio Ruiz <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> wondering if there is a way to do this.. > > say we have a model that has attributes length and width.. > > :length and :width > > and we have a class square: > > class Square < ActiveRecord::Base > > def area > area = length * width > end > > end > > is there a way i can do a: > > Square.find(:area => ''144'') > > to produce any squares with an area of 144? > > i know there are lots of other ways to do this, but in the case i am > working on, a find would be REALLY clean..Not directly (although clearly you could write Square.where(''length * width = 144''), or define a named scope that does this. you also can''t use indexes for queries like this (unless your db supports functional indexes) 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.
On Thu, Apr 28, 2011 at 9:34 PM, Sergio Ruiz <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> wondering if there is a way to do this.. > > say we have a model that has attributes length and width.. > > :length and :width > > and we have a class square: > > class Square < ActiveRecord::Base > > def area > area = length * width > end > > end > > is there a way i can do a: > > Square.find(:area => ''144'') > >You can always use your db''s arithmetic operators, for mysql you can look at http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html> to produce any squares with an area of 144? > > i know there are lots of other ways to do this, but in the case i am > working on, a find would be REALLY clean.. > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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.
thanks, guys! i ended up just writing a method for the model that does the check.. it''s something that would only happen maybe once a week.. and then, maybe only one or two transactions. -- Posted via http://www.ruby-forum.com/. -- 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.