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.