Hello, I would like to know the more elegant way to add a method for an array of objects from the database. Let me give an example : Class Product .... end And in my controller : @products = Product.find(:all) And now I want to define a method which I could like @products.select_fruits having said somewhere : def select_fruits .... end Thank you very much, Pierre --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 29, 2007, at 11:23 AM, Pierre Valade wrote:> I would like to know the more elegant way to add a method for an array > of objects from the database. > > Let me give an example : > > Class Product > .... > end > > And in my controller : > > @products = Product.find(:all) > > And now I want to define a method which I could like > > @products.select_fruits > > having said somewhere : > > def select_fruits > .... > endIs you question about where those methods should go? They can, and usually should, go in the model. class Product def self.find_all find(:all, .....) end def self.select_fruits(parameters) find(:all, :conditions... end end --- controller -- fruits = Product.select_fruits(:fruit_type = params[:....]) -- def gw acts_as_n00b writes_at(www.railsdev.ws) end --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you for your answer but my question is how to add method to an array of @products. I have : @products.find(:all) Then, if I want to select only fruits from @products, I could do : @products.select {...} And it''s this select that I want to put in a method select_fruits so that I could do : @products.select_fruits which corresponds to @products.select { ... } Thank you, Pierre On 29 nov, 20:48, Greg Willits <li...-0Bv1hcaDFPRk211Z5VL+QA@public.gmane.org> wrote:> On Nov 29, 2007, at 11:23 AM, Pierre Valade wrote: > > > > > I would like to know the more elegant way to add a method for an array > > of objects from the database. > > > Let me give an example : > > > Class Product > > .... > > end > > > And in my controller : > > > @products = Product.find(:all) > > > And now I want to define a method which I could like > > > @products.select_fruits > > > having said somewhere : > > > def select_fruits > > .... > > end > > Is you question about where those methods should go? They can, and > usually should, go in the model. > > class Product > > def self.find_all > find(:all, .....) > end > > def self.select_fruits(parameters) > find(:all, :conditions... > end > > end > > --- controller -- > > fruits = Product.select_fruits(:fruit_type = params[:....]) > > -- > def gw > acts_as_n00b > writes_at(www.railsdev.ws) > end--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
One way is to make your own finder directly on the model class Product < ActiveRecord::Base def self.find_all_fruits self.find :all, :condition => ["category = "fruit"] end end Although you don''t have to bother if you can tell by looking at a field. For example, if you have a "category" column that shows "fruit", you can just use a dynamic finder for that @fruit = Product.find_all_by_category("fruit") @fruit_in_stock = Product.find_all_by_category_and_available("fruit",true) You should be letting your database do the work of filtering records, as that''s its job. You don''t want to iterate and reject / filter records in Ruby. It''s much slower. It looks cool but it''s not worth it. On Nov 29, 2007 1:52 PM, Pierre Valade <pierre.valade-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thank you for your answer but my question is how to add method to an > array of @products. > > I have : @products.find(:all) > > Then, if I want to select only fruits from @products, I could do : > @products.select {...} > And it''s this select that I want to put in a method select_fruits so > that I could do : > > @products.select_fruits which corresponds to @products.select { ... } > > Thank you, > Pierre > > On 29 nov, 20:48, Greg Willits <li...-0Bv1hcaDFPRk211Z5VL+QA@public.gmane.org> wrote: > > On Nov 29, 2007, at 11:23 AM, Pierre Valade wrote: > > > > > > > > > I would like to know the more elegant way to add a method for an array > > > of objects from the database. > > > > > Let me give an example : > > > > > Class Product > > > .... > > > end > > > > > And in my controller : > > > > > @products = Product.find(:all) > > > > > And now I want to define a method which I could like > > > > > @products.select_fruits > > > > > having said somewhere : > > > > > def select_fruits > > > .... > > > end > > > > Is you question about where those methods should go? They can, and > > usually should, go in the model. > > > > class Product > > > > def self.find_all > > find(:all, .....) > > end > > > > def self.select_fruits(parameters) > > find(:all, :conditions... > > end > > > > end > > > > --- controller -- > > > > fruits = Product.select_fruits(:fruit_type = params[:....]) > > > > -- > > def gw > > acts_as_n00b > > writes_at(www.railsdev.ws) > > end > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > @products = Product.find(:all) > > And now I want to define a method which I could like > > @products.select_fruits > > having said somewhere : >module Foo def select_fruits # some code here end end # meanwhile.. back on the farm.. @products = Product.find(:all) @products.extend Foo =================== Or even better.. ========================class Product belongs_to :fruit, :extends => Foo end Look for "Association extensions" within the Rails api.. hth.. ilan -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks you ! It''s great ! Pierre On 29 nov, 22:37, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > @products = Product.find(:all) > > > And now I want to define a method which I could like > > > @products.select_fruits > > > having said somewhere : > > module Foo > def select_fruits > # some code here > end > end > > # meanwhile.. back on the farm.. > @products = Product.find(:all) > @products.extend Foo > > =================== Or even better.. ========================> class Product > belongs_to :fruit, :extends => Foo > end > > Look for "Association extensions" within the Rails api.. > > hth.. > > ilan > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---