rails migration update_attributes bug when id is false

539 views Asked by At

Here I describe a potential bug in rails 4.1.6

when create a table without id

def change
    create_table :tests, id:false do |t|
    ......
end

the update_attributes function will fail

for example, in console

> a=Test.first
=> #<Test symbol: "ABC", month: 3, year: 14>
> b={"symbol"=>"EFG", "month"=>"4", "year"=>"15"}
.... 
> a.update_attributes(b)
TypeError: nil is not a symbol
.......

detailed check show this is caused by an additional field "nil=>nil" that was automatically inserted in to Test. For example

> a.attributes
=> {"symbol"=>"ABC", "month"=>3, "year"=>14, nil=>nil}

The nil=>nil field was also generated in the attributes of new record. For example

> b=Test.new
=> #<Test symbol: nil, month: nil, year: nil>
> b.attributes
=> {"symbol"=>nil, "month"=>nil, "year"=>nil, nil=>nil}

BYW, I am using mysql. The above issue does not exist when id:false was removed

1

There are 1 answers

1
Hoa On BEST ANSWER

It requires a primary key in the related table for Rails to work correctly. Create a migration which sets a field (for example, the symbol field) or a set of fields as a primary key then your code will work as usual.