This project is archived and is in readonly mode.
Problems when working with a model which has a non-default primary key, but still has a field named "id"
Reported by Paul Schyska | November 12th, 2010 @ 03:19 PM
We have a model that has a custom primary key
("transaction_id"). It additionally has a column called "id", which
is just an integer column.
On this model, if i set either transaction_id or id for a record,
using both model.id= and model['id'] syntax resp.
model.transaction_id= and model['transaction_id'], it sets the
value for both columns. For example, when I set id to 42,
transaction_id will also be set to 42.
At https://gist.github.com/674194 you will find a rails console session demonstrating this.
Comments and changes to this ticket
-
Cesario November 13th, 2010 @ 12:09 AM
I just had a look at that and I believe this is actually the expected behavior.
activerecord/attribute_methods/write.rb
#L19:# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+. Empty strings # for fixnum and float columns are turned into +nil+. def write_attribute(attr_name, value) attr_name = attr_name.to_s attr_name = self.class.primary_key if attr_name == 'id'
If I understand correctly the last line, we can't use the
id
attribute at all if we don't want to use it. -
rails February 13th, 2011 @ 04:36 PM
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
rails February 13th, 2011 @ 04:36 PM
- State changed from open to stale
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>