In a migration is a table without id column created:
create_table :drivers_companies, :id => false do |t|
t.references :driver
t.references :company
t.string :last_sessionid
end
When saving a record:
dc = DriversCompany.where(company_id: 1, driver_id: 5)
if dc.length>0
dc[0].last_sessionid = req.sessionID
dc[0].save!
end
I get an error:
Mysql2::Error: Unknown column 'drivers_companies.' in 'where clause': UPDATE 'drivers_companies' SET 'last_sessionid' = 'bth49sv0outsehcg0ribuiu4h' WHERE 'drivers_companies'.'' IS NULL
How to update a record in table without ID column?
ActiveRecord needs a primary key to do this. That can be a problem with legacy tables which can not be changed. You can not solve this without changing the table.
Sometimes one can use the "Composite Primary Keys for ActiveRecords" gem to solve it: https://github.com/composite-primary-keys/composite_primary_keys
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With