This project is archived and is in readonly mode.

#993 ✓resolved
Denis

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

Reported by Denis | September 8th, 2008 @ 02:39 AM | in 2.x

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.

Comments and changes to this ticket

  • Denis

    Denis September 8th, 2008 @ 02:55 AM

    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...

  • Frederick Cheung

    Frederick Cheung September 8th, 2008 @ 12:05 PM

    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.

  • Frederick Cheung

    Frederick Cheung December 10th, 2008 @ 11:13 PM

    • State changed from “new” to “resolved”

    Should be fixed by #1551 - overriding transaction shouldn't barf stuff anymore

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages