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.

Problems when working with a model which has a non-default primary key, but still has a field named "id"

#5961

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.

Reported by Paul Schyska · November 12th, 2010 @ 03:19 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Cesario
    Cesario

    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.

    November 13th, 2010 @ 12:09 AM

  2. rails
    rails
    • 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.

    February 13th, 2011 @ 04:36 PM

  3. rails
    rails
    • State changed from open to stale

    February 13th, 2011 @ 04:36 PM