2

In my console I save models like this:

billy = Student.first
billy.name = 'Billy'
billy.save

Works as expected. However, after I call an ApplicationMailer in the console, the above mechanism no longer works :(

MyMailer.send_some_emails
billy.name = 'Jimmy'
billy.save # returns true
billy.save! # no exception thrown
billy.reload
billy.name # will return 'Billy', not 'Jimmy' as expected

Any ideas?

at.
  • 50,922
  • 104
  • 292
  • 461
  • Maybe it will help to enable SQL logging and see if the correct data is sent to the database? `ActiveRecord::Base.logger = Logger.new(STDOUT)`. – shock_one Feb 10 '15 at 21:04
  • We actually do have SQL logging enabled and in the first case, we see the SQL `UPDATE` statement output. In the second case though, there is nothing sent to the database. We just see `begin transaction` followed immediately by `commit transaction` – at. Feb 10 '15 at 21:05
  • Try these couple of things: In pry `show-method billy.save` to make sure it's not redefined. `billy.update_attributes(name: 'Jimmy')` to see if other persisting methods work. Save another instance of this model. Save an instance of another model. – shock_one Feb 10 '15 at 21:11
  • @muistooshort - that SQL statement works and is only shown before sending the emails, that SQL statement is not sent for the saving after sending the emails – at. Feb 11 '15 at 01:35

1 Answers1

0

I have run into this a couple of times. It's always been a case of having set attr_accessor for that attribute in its respective model. Without seeing more of your code I can't tell you for sure, but I bet you have something like this.

class Student < ActiveRecord::Base
  attr_accessor :name
  #code...
end

Check out the accepted answer to a similar question for more details. But essentially you need to remove the attr_accessor :name line.

Community
  • 1
  • 1
hetre85
  • 39
  • 8
  • I have a few `attr_accessor`s, but they're all for completely unrelated fields (having to do with devise). `name` is not referenced. – at. Jun 02 '15 at 04:03