On Mar 2, 2:59 pm, Fernando Perez <[email protected]> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to