On Oct 26, 1:27 pm, drewB <[email protected]> wrote:
> On Oct 26, 9:51 am, Marnen Laibow-Koser <rails-mailing-l...@andreas-
>
> s.net> wrote:
> > Wes wrote:
> > > Try this:
>
> > > class ObjectivesController < ApplicationController
> > >   before_filter :require_user
> > >   layout false
> > >   def create
> > >     objective = current_user.objectives.create(params[:objective])
> > >   end
> > > end
>
> > No!  Create shouldn't be a controller method.
>
> Why not?

There's no reason not to. In fact it's a Rails convention to do so.

> > > And as others have said, don't overwrite
>
> > over*ride*
>
> > > AR class methods like
> > > 'create', otherwise you will enter a world of pain.
>
> Thanks for the good practice tip.
>
>
>
>
>
> > Yeah.  If you must override, it's probably best to do something like
>
> > def create(attributes, user_id)
> >   super(attributes.merge :user_id => user_id)
> > end
>
> > but even this is kind of bad.
>
> > > Also, you are deleting a param called 'freq_options', if the model
> > > does not need it then it shouldn't be in the hash. If the controller
> > > needs it, then pass it as params[:freq_options] rather than params
> > > [:objective][:freq_options]. If it's not used there, then why pass it
> > > at all?
>
> > Good catch!
>
> It will eventually be used, just not yet.

Ok, if you intend to use it later, then you can just specify an
accessor in your model, that way you can pass it in without error.

class Objective < ActiveRecord::Base
  attr_accessor :freq_options
end

Then later if you add this as a db attribute, you can remove the
accessor. Otherwise you may want to do some manipulation of other data
in the model based on the value of this accessor (via callbacks).

-- W

>
>
> > > -- W
>
> > Best,
> > --
> > Marnen Laibow-Koserhttp://www.marnen.org
> > [email protected]
> > --
> > 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