Michael Moulsdale
Michael Moulsdale

Reputation: 1488

rails model.save returns true but does not save to db

I have a function within a model that performs some actions after_save

Within that function I have the following code

progression = Progression.find_by_id(newprogression)
if progression.participation_id.nil?
  progression.participation_id = participation.id
  progression.save
else

What I am seeing is that progression is not being updated. Even though the following

any thoughts?

Upvotes: 1

Views: 2920

Answers (5)

cbrwizard
cbrwizard

Reputation: 93

Ok, I had the same problem and I managed to deal with it. The problem was with association name. I had a model named User and a child model named Update, which had number of new updates of each user. So, in user.rb I had this line:

has_one :update

After I deleted this line, everything started working again. Looks like this is a rails reserved name and it should not be used when creating models. Perhaps you have some kind of the same problem here. It's a pity that Rails doesn't indicate this problem.

Upvotes: 1

wedens
wedens

Reputation: 1830

save and after_save run in the same transaction. maybe it's a problem. try to use after_commit callback

Upvotes: 0

kobaltz
kobaltz

Reputation: 7070

Try

progression = Progression.find_by_id(newprogression)
if progression.participation_id.nil?
  progression.update_attributes(
     :participation_id => participation.id
  )
else

Upvotes: 0

Goldentoa11
Goldentoa11

Reputation: 1760

I'm still pretty new to Rails, but every time I save something, I have to use a bang method. Have you tried progression.save!

Upvotes: 1

lewstherin
lewstherin

Reputation: 73

It might be a caching problem. From the rails guide on AR Relations - Sec 3.1, you might want to reload the cache by passing true. You are likely to encounter this, if you are checking it via participation, after establishing the link through progression.

Assuming Progression belongs_to Participation,

Can you try: progression.participation = participation and then progression.save

Also, try: participation.progressions << progression and participation.save ?

Upvotes: 0

Related Questions