Understanding rails transactions execution

Hi,

I’d like to understand how Rail’s handles rollbacks. Say I have the
following:

def action(…)

User.transaction do
user1 = User.new
user1.attr1 = 5
relationship = Relationship.new
if user1.save
…do stuff…
end

if relationship.save
  ...do stuff...
end

end
end

My question is, say user1.save did not go through due to some UNIQUE
constraint violoation in the db, does Rails continue execution to
relationship.save? Or does it throw an exception, and if so, what’s the
exception that it throws to tell us that it did a ROLLBACK. Since I’m
doing a user1.save without the ‘user1.save!’, instead of throwing an
exception, it’ll return False. So if that’s the case, does it just
continue execution with relationship.save?

Thanks,

Sam

On 10/1/06, Sam D. [email protected] wrote:

user1 = User.new

end

My question is, say user1.save did not go through due to some UNIQUE
constraint violoation in the db, does Rails continue execution to
relationship.save? Or does it throw an exception, and if so, what’s the
exception that it throws to tell us that it did a ROLLBACK. Since I’m
doing a user1.save without the ‘user1.save!’, instead of throwing an
exception, it’ll return False. So if that’s the case, does it just
continue execution with relationship.save?

The database adapter would raise ActiveRecord::StatementInvalid. Your
transaction block would rescue the exception, rollback the db
transaction,
and reraise the exception.

jeremy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs