ActiveRecord Transaction Rollback not happening/Postgres

I have these settings using postgresql gem with Postgres 8.4 Installed:
development:
adapter: postgresql
host: localhost
database: db_development
username: postgres
password: postgres
encoding: utf8

Code snippet in lib/ rake task:
Benchmark.bm(7) do |x|
res = x.report(“localdb:populate”) do
ruby_obj = YAML.load_file(“lib/tasks/populatedb.yml”)

    ruby_obj.each_key do |key|
      h = ruby_obj[key]
      begin
        ActiveRecord::Base.transaction do
          msg = Message.new
          msg.name= h['name']
          msg.message_key= h['message_key']
          msg.category= h['category']
          msg.displayable=true if h['displayable']
          msg.save
        end
      rescue => e
        logger.info "Error happened #{e.backtrace}"
      end

    end
  end

the populatedb.yml just has some yml Message key.
Message is a ActiveRecord Object.
It has validation on message_key.

I intentionally want that either all messages get populated or none and
to check that I make message_key same for 2 messages with
validates_uniqueness_of :message_key.

My problem is that even is the same key exists in populatedb.yml data,
everything works well.
No rollback.

Is this code correct?
ActiveRecord::Base.transaction do
msg = Message.new
msg.name= h[‘name’]
msg.message_key= h[‘message_key’]
msg.category= h[‘category’]
msg.displayable=true if h[‘displayable’]
msg.save
end

Do I need to do anything special in Postgres Installation DB Set up?

Does the postgres adapter handle this situation?

Thanks in advance.

On Feb 12, 4:26 pm, Rohit S. [email protected] wrote:

          msg.category= h['category']
          msg.displayable=true if h['displayable']
          msg.save
        end

Do I need to do anything special in Postgres Installation DB Set up?

Well your transaction only wraps a single insert, so only that insert
could get rolled back. Furthermore, save doesn’t raise an exception if
saving fails (save! does) so you’d never get a rollback anyway.

Fred

Frederick C. wrote:

On Feb 12, 4:26�pm, Rohit S. [email protected] wrote:

� � � � � � � msg.category= h[‘category’]
� � � � � � � msg.displayable=true if h[‘displayable’]
� � � � � � � msg.save
� � � � � � end

Do I need to do anything special in Postgres Installation DB Set up?

Well your transaction only wraps a single insert, so only that insert
could get rolled back. Furthermore, save doesn’t raise an exception if
saving fails (save! does) so you’d never get a rollback anyway.

Fred

I actually changed this as follows after the post and it does work. The
ROLLBACK shows in the log also.
Thanks Fred though. SIlly to overlook it. Also, the ROLLBACK happens at
the validation level itself. I don’t have to throw the exception or even
catch it.

begin
ActiveRecord::Base.transaction do
ruby_obj.each_key do |key|
h = ruby_obj[key]
msg = Message.new
msg.name= h[‘name’]
msg.message_key= h[‘message_key’]
msg.category= h[‘category’]
msg.displayable=true if h[‘displayable’]
msg.save!
end
end
rescue => e
logger.info “Error happened #{e.backtrace}”
#throw e
end

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