Fernando Perez
2011-Mar-02 14:59 UTC
How to create a method such as: @user.travels.register(.)
Hi, In order to trim my controllers down and keep the code in the correct place, I am trying to refactor: @user.travels.create(:name => ..., :date => ..., :ip => request...., etc) to: @user.travels.register(params, request) So my questions are: 1) is create() an instance method of Travel? I have doubts, since Travel.create is a class method. 2) how to write my register method() so that it behaves like create()? Thanks for your help. -- 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.
Frederick Cheung
2011-Mar-02 15:10 UTC
Re: How to create a method such as: @user.travels.register(.)
On Mar 2, 2:59 pm, Fernando Perez <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > In order to trim my controllers down and keep the code in the correct > place, I am trying to refactor: > > @user.travels.create(:name => ..., :date => ..., :ip => request...., > etc) > > to: > > @user.travels.register(params, request) > > So my questions are: > > 1) is create() an instance method of Travel? I have doubts, since > Travel.create is a class method. >it''s not. It''s a method on the association proxy> 2) how to write my register method() so that it behaves like create()?There are 2 possible ways to go. The first is the associations are scopes: if you have a class method on Travel called foo, then user.travels.foo calls the foo class method but active record calls will be scoped to the travels for that user. The second is association extensions. If you do has_many :foos do def bar end end then you will be able to call user.foos.bar. There''s some more info here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Fred> > Thanks for your help. > > -- > 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-/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.
Fernando Perez
2011-Mar-02 15:17 UTC
Re: How to create a method such as: @user.travels.register(.)
Thank you Frederick. I now know where and what to look for. -- 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.