Notes posted by ssorokaRSS feed
@wiseleyb and @glosakti, neither of your suggestions are necessary, and both are bad practices.
test "transactions" do assert_raises ZeroDivisionError do User.transaction do 1/0 end end end
passes just fine on its own, with the transaction rolled back as you’d expect. No need to hack something ugly together.
for some reason this method only works on relation objects, not directly on an AR class.
there’s an closed ticket for this here http://rails.lighthouseapp.com/projects/8994/tickets/5688-modeloffsetxlimitx-unknown-offset-method-exception and should be resolved in the next release of rails.
add_index is a different method. I think this is just a bug and it’s broken.
Don’t use this method unless you’re sure your string isn’t nil. Instead use the raw() method, which wont raise an exception on nil.
Just a note, ypetya’s idea of using a before filter to set the primary key wont scale. transactions will eventually step on each other and probably end up with duplicate key ids, unless you have some other method to ensure uniqueness.
You’d be better off using mysql to generate the default integer primary key and have a secondary string “key” field.
This doesn’t work for me. I do something like:
create_table :user_follows, :force => true do |t| t.references :user t.references :followed_user t.timestamps t.index :user t.index :followed_user end
and I get:
rake aborted! An error has occurred, all later migrations canceled: undefined method `index' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x106c02220>
add_index has the same effect.