0

So, i have form that updates a record in the database. In my controllers update action i want to change a value to something if another one of the values is Estimate. Maybe this will make more sense...this is what i'm trying to do.

def update
    @invoice = Invoice.find(params[:id])
    if @invoice.update_attributes(params[:invoice])
        [email protected] == "Estimate"
            # if the value of status is Estimate then change the
            # value of estimate_sent_date to the current timestamp
        end
        redirect_to invoices_path
    else
        render 'edit'
    end
end

The only values of the form that i'm concerned with are status and estimate_sent_date. Mostly, i'm just not sure how to change the value of estimate_sent_date and save that record.

Also, should i save everything and then do a separate call to save the estimate_sent_date or just save it all at once? I guess i could change the value of estimate_sent_date before calling if @invoice.update_attributes(params[:invoice]), couldn't i?

Thanks for the help!

3 Answers 3

5

As Ryan Bigg says, state machine does work here. An alternative solution is to use a before_save callback on the Invoice model, like so:

before_save :set_sent_date

def set_sent_date
  if self.status_changed? && self.status == "Estimate"
     self.estimate_sent_date = Time.now
  end
end
3
  • Thanks! This may be the best route for me. The state_machine gem looks interesting but it may be more than i need since this is just a personal application anyway.
    – ryanpitts1
    Nov 5, 2012 at 3:09
  • 1
    Yep, state_machine can be overkill sometimes. Learn more about active record callbacks here: api.rubyonrails.org/classes/ActiveRecord/Callbacks.html Nov 5, 2012 at 3:11
  • Workflow is probably the 'new-cool' in state machine gems. It seems to be actively maintained vs state_machine which appears abandoned. Feb 19, 2015 at 21:40
2

I would move this kind of business logic in your Invoice model. This is a typical use case for a :before_save callback

1
  • 1
    Please provide examples when answering questions.
    – Ryan Bigg
    Nov 5, 2012 at 5:00
1

It sounds like you're trying to re-invent a state machine here. I would recommend looking into the state_machine gem and then using it to implement an event that happens after an invoice transitions to the 'estimate' state, placing this inside the state_machine definition which would go inside your model:

after_transition :to => :estimate do |invoice|
  invoice.estimate_sent_date = Time.now
end
3
  • I'm guessing i would put that in my model?
    – ryanpitts1
    Nov 5, 2012 at 3:06
  • 3
    yes, this goes in the model. good practice to keep the controllers skinny! Nov 5, 2012 at 3:09
  • Updated answer to reflect that the code should go into the model.
    – Ryan Bigg
    Nov 5, 2012 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.