Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Having column named 'transaction' in a model leads to weird behavior

#993

While the doc warns not to use associations that are named the same as methods in AcitiveRecord::Base, I could not find any warning about column names. But using a column named 'transaction' leads to strange behavior.

Create a model with a column named 'transaction', e.g.


class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.string :transaction

      t.timestamps
    end
  end
end

class Item < ActiveRecord::Base
end

Create instances of the class in console:


$ script/console
Loading development environment (Rails 2.1.1)
>> Item.create!
=> #<Item id: 1, transaction: nil, created_at: "2008-09-08 01:30:14", updated_at: "2008-09-08 01:30:14">
>> Item.create!
=> #<Item id: nil, transaction: nil, created_at: nil, updated_at: nil>
>>

Note the second (and all subsequent) attempt to create and persist an instance creates the instance but does not save it to the database.

If the column is renamed to something else, this problem does not happen.

Reported by Denis · September 8th, 2008 @ 02:39 AM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Denis
    Denis

    Some kind soul in #rubyonrails pointed out that there is a transaction method, courtesy of ActiveRecord::Transactions. Still, it would be nice if ActiveRecord could somehow warn us we're doing something stupid...

    September 8th, 2008 @ 02:55 AM

  2. Frederick Cheung
    Frederick Cheung

    Associations and columns boil down to the same thing since both generate methods with the same name as the association/column.

    ActiveRecord already has a a thing to check for 'dangerous' column names, not sure why it didn't do anything in this case.

    September 8th, 2008 @ 12:05 PM

  3. Frederick Cheung
    Frederick Cheung
    • State changed from new to resolved

    Should be fixed by #1551 Don't use the transaction instance method - overriding transaction shouldn't barf stuff anymore

    December 10th, 2008 @ 11:13 PM