On Jul 25, 10:43 am, Linus Pettersson <[email protected]>
wrote:
> Hi!
>
> I have a form with a checkbox called "status" which is a boolean in the
> database.
>
> In my controller's update action I want to check if the checkbox has
> changed.

No, you really shouldn't be doing that in the controller. You almost
certainly want to do this in a before_save (or possibly
before_validation) callback on the MODEL.

Several reasons:

- it's a best practice to keep business logic in the model ("fat
model / skinny controller")

- inside the model callbacks, you've got access to both typecast data
(the incoming status will already have the right type) and the
_changed? predicate. For instance, your code could be shortened to:

class Item < ActiveRecord::Base

  before_save :update_position

  def update_position
    self.position = nil if status_changed?
  end

end

Not only does it avoid the messy conversion, it (IMHO) is far more
readable.

--Matt Jones

-- 
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