1

I have a model that won't update properly with update_attributes, but will update using update_column. I'm assuming this is because a callback is interfering. Unfortunately, it's not throwing any errors, so I can't figure out where exactly the problem is coming from.

Is there a way to trace callbacks so I can go through them, one by one, until I find the culprit?

nullnullnull
  • 8,039
  • 12
  • 55
  • 107

2 Answers2

1

The API documentation shows how you can access the callback chain.

Here's some one liners that you can use in your console that should give you the idea:

# Print before_validate callbacks
Post._validate_callbacks.select { |cb| cb.kind.eql? :before }.each { |cb| puts cb.filter }

# Print after_update callbacks
Post._update_callbacks.select { |cb| cb.kind.eql? :after }.each { |cb| puts cb.filter }

Remember that updates to models will also call save so it's a good idea to trawl through them all.

-1

Check to see that the params you are passing to the update_attributes() method are mass assignable.

They should be defined as :attr_accessible in your rails model otherwise they will be stripped out before saving.

class Widget < ActiveRecord::Base
  attr_accessible :name
end

More info here http://guides.rubyonrails.org/security.html

ADAM
  • 3,903
  • 4
  • 29
  • 45
  • Yep, they are mass-assignable. Any tips on tracing callbacks? – nullnullnull Jan 20 '13 at 01:17
  • Hard to say without looking at your model, but I would maybe try and set each param individually and see if it works and check that @obj.save returns true and @obj.valid? is true. – ADAM Jan 20 '13 at 01:32
  • I asked another question about this problem earlier, which you can find [here](http://stackoverflow.com/questions/14415857/rails-update-column-works-but-not-update-attributes). That post goes into detail about this specific issue, including the model and controller. After tinkering with this all day, it seems like callbacks are the most likely culprit, and I really want to just hone in on them. The behavior I'm getting is extremely odd, and both @obj.save and @obj.valid? return true. I'm only updating a single param, and even if I call update_attribute(:body, params[reply][body]), I get nada. – nullnullnull Jan 20 '13 at 01:42