2
votes

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?

1
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
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.
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
@shock_one - I'll try that - at.
@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.

1 Answers

0
votes

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.