I need a method, I call it "get_or_create(item_name)". When an Model.get_or_create(item_name) is called, Model.find_by_name(item_name) will be performed fist, if there is an item with the name, the item is return, or a new item with the name will be created and returned. Because most of the ActiveRecord models will need this method, I try to extend it into ActiveRecord, and put the codes into /lib/get_or_create.rb, and require it from environment.rb.>require ''get_or_create'' >class ActiveRecord::Base > include GetOrCreate >endOnly one problem left... Can anyone tell me how to write the get_or_create.rb ? -- 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 -~----------~----~----~----~------~----~------~--~---
On 6 Dec 2007, at 10:49, Nanyang Zhan wrote:> > I need a method, I call it "get_or_create(item_name)". When an > Model.get_or_create(item_name) is called, > Model.find_by_name(item_name) > will be performed fist, if there is an item with the name, the item is > return, or a new item with the name will be created and returned. > > Because most of the ActiveRecord models will need this method, I try > to > extend it into ActiveRecord, and put the codes into > /lib/get_or_create.rb, and require it from environment.rb. >> require ''get_or_create'' >> class ActiveRecord::Base >> include GetOrCreate >> end > > Only one problem left... Can anyone tell me how to write the > get_or_create.rb ?You mean like the find_or_create_by_ dynamic finders that ActiveRecord already has? Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 6 Dec 2007, at 10:49, Nanyang Zhan wrote: > >> /lib/get_or_create.rb, and require it from environment.rb. >>> require ''get_or_create'' >>> class ActiveRecord::Base >>> include GetOrCreate >>> end >> >> Only one problem left... Can anyone tell me how to write the >> get_or_create.rb ? > > You mean like the find_or_create_by_ dynamic finders that ActiveRecord > already has? > > FredI was just going to say that, Fred. -- 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 -~----------~----~----~----~------~----~------~--~---
Alvaro Perez wrote:> Frederick Cheung wrote: >> You mean like the find_or_create_by_ dynamic finders that ActiveRecord >> already has? >> >> Fred > > I was just going to say that, Fred.Thanks! I didn''t know there is such a method already. Anyway, can you tell me how to write these methods on my own? You can take the exact same one for example. -- 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 -~----------~----~----~----~------~----~------~--~---
If you were going to write such a method yourself, you''d want to add it to your model''s class, not ActiveRecord. So inside app/models/ model.rb: class Model < ActiveRecord::Base ... def self.find_or_create_by_name(name) find_by_name(name) || create(:name => name) end ... end Remember that when a method definition begins with "self.", it means you''re creating a class method, not an instance method. So you''re operating within the scope of the class, and thus don''t have to prefix the find_by_name() and create() calls with "Model.", since it''s implied. P.S. Another style of defining class methods you''ll see in some people''s code: class Model < ActiveRecord::Base ... class << self def find_or_create_by_name(name) find_by_name(name) || create(:name => name) end end ... end As far as I can deduce, these two different styles are functionally identical and merely a matter of preference. On Dec 6, 3:31 am, Nanyang Zhan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Alvaro Perez wrote: > > Frederick Cheung wrote: > >> You mean like the find_or_create_by_ dynamic finders that ActiveRecord > >> already has? > > >> Fred > > > I was just going to say that, Fred. > > Thanks! I didn''t know there is such a method already. > > Anyway, can you tell me how to write these methods on my own? You can > take the exact same one for example. > -- > 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 -~----------~----~----~----~------~----~------~--~---
On Dec 6, 2007, at 11:30 AM, Ian Lesperance wrote:> > If you were going to write such a method yourself, you''d want to add > it to your model''s class, not ActiveRecord. So inside app/models/ > model.rb: >I believe the OP wanted to add this to ActiveRecord because he wanted the same functionality for all of his models. Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Touche. In that case, it''s probably worth noting that it''d be better if it were written as a plug-in, rather than opening up ActiveRecord::Base directly from environment.rb. On Dec 6, 9:48 am, Phillip Koebbe <phillipkoe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 6, 2007, at 11:30 AM, Ian Lesperance wrote: > > > > > If you were going to write such a method yourself, you''d want to add > > it to your model''s class, not ActiveRecord. So inside app/models/ > > model.rb: > > I believe the OP wanted to add this to ActiveRecord because he wanted > the same functionality for all of his models. > > Peace, > Phillip--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ian Lesperance wrote:> Touche. In that case, it''s probably worth noting that it''d be better > if it were written as a plug-inYes, Phillip was right, I wanted such a method could be accessed in all ActiveRecord models. Can you show me how to write such a plugin? I find no guide to extend the ActiveRecord with this kind of method. -- 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 -~----------~----~----~----~------~----~------~--~---
On Dec 8, 2007, at 4:58 AM, Nanyang Zhan wrote:> Ian Lesperance wrote: >> Touche. In that case, it''s probably worth noting that it''d be better >> if it were written as a plug-in > > Yes, Phillip was right, I wanted such a method could be accessed in > all > ActiveRecord models. > Can you show me how to write such a plugin? I find no guide to extend > the ActiveRecord with this kind of method.Start here: http://nimrod.interinter.net/plugins/trunk/acts_as_monkey/ http://wiki.rubyonrails.org/rails/pages/HowToWriteAnActsAsFoxPlugin http://nubyonrails.com/articles/2006/05/04/the-complete-guide-to- rails-plugins-part-i http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to- rails-plugins-part-ii Other links: http://wiki.rubyonrails.org/rails/pages/HowTosPlugins -- 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 -~----------~----~----~----~------~----~------~--~---
I thought the OP only wanted a find_or_create_by_* method or I could be mistaken. He didn''t want to write a plugin. OP, there is already a find_or_create_by method built-in to ActiveRecord. Model.find_or_create_by_name(:name => "Name") More information here: http://www.noobkit.com/show/ruby/rails/rails-edge/activerecord-edge/activerecord/base.html#toc-dynamic-attribute-based-finders On Dec 9, 2007 7:41 AM, Greg Willits <lists-0Bv1hcaDFPRk211Z5VL+QA@public.gmane.org> wrote:> > On Dec 8, 2007, at 4:58 AM, Nanyang Zhan wrote: > > > Ian Lesperance wrote: > >> Touche. In that case, it''s probably worth noting that it''d be better > >> if it were written as a plug-in > > > > Yes, Phillip was right, I wanted such a method could be accessed in > > all > > ActiveRecord models. > > Can you show me how to write such a plugin? I find no guide to extend > > the ActiveRecord with this kind of method. > > Start here: > > http://nimrod.interinter.net/plugins/trunk/acts_as_monkey/ > http://wiki.rubyonrails.org/rails/pages/HowToWriteAnActsAsFoxPlugin > > http://nubyonrails.com/articles/2006/05/04/the-complete-guide-to- > rails-plugins-part-i > http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to- > rails-plugins-part-ii > > > Other links: > > http://wiki.rubyonrails.org/rails/pages/HowTosPlugins > > -- > def gw > acts_as_n00b > writes_at(www.railsdev.ws) > end > > > > > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---