This project is archived and is in readonly mode.

#2314 ✓invalid
James B. Byrne

v 2.3.2 - appropriates attributes ending in _type and _value

Reported by James B. Byrne | March 23rd, 2009 @ 05:27 PM | in 2.x

If a table has attributes ending in type or value then these attributes cannot be set.

Example:


class CreateIdentifiers < ActiveRecord::Migration
  def self.up
    create_table :identifiers do |t|
      t.integer :entity_id
      t.string :code_type,  :null => false
      t.string :code_value, :null => false
    end
  end

script/console

mi = me.identifiers.build => #mi.code_type = "Xray" => "Xray" mi.code_type => nil mn = Identifier.new => #mn.entity_id = 1 => 1 mn.code_type = "Whisky" => "Whisky" mn => #



If the write_attribute method is used instead, then the values are set but any attribute= overrides are skipped.

Example:

class Identifier < ActiveRecord::Base belongs_to :entity

def code_type=(code)

code.upcase

end

def code_value=(desc)

desc.downcase

end end

script/console

mn.write_attribute("code_type", "MixCase") => "MixCase" mn => #

Comments and changes to this ticket

  • James B. Byrne

    James B. Byrne March 23rd, 2009 @ 05:31 PM

    Lost example added:

    Using write_attribute skips attribute= processing in model.

    
    >> mn.write_attribute(code_type, "MixCase")
    NameError: undefined local variable or method `code_type' for #<Object:0x2b1bc50203f8>
            from (irb):19
    >> mn.write_attribute("code_type", "MixCase")
    => "MixCase"
    >> mn
    => #<Identifier id: nil, entity_id: 1, code_type: "MixCase", code_value: nil>
    >> 
    
  • James B. Byrne

    James B. Byrne March 23rd, 2009 @ 05:32 PM

    • Title changed from “v 2.3.2 - appropriates attributed ending in _type and _value” to “v 2.3.2 - appropriates attributes ending in _type and _value”
  • James B. Byrne

    James B. Byrne March 23rd, 2009 @ 05:36 PM

    mi = me.identifiers.build 
    => 
    mi.code_type = "Xray" 
    => "Xray" 
    mi.code_type 
    => nil 
    mn = Identifier.new 
    => mn.entity_id = 1 
    => 1 
    mn.code_type = "Whisky" 
    => "Whisky" 
    mn,code_type
    => nil
    
  • James B. Byrne

    James B. Byrne March 23rd, 2009 @ 07:44 PM

    This appears to be dependent upon whether or not the model name forms part of the attribute name. So:

    mi = Identity.new
    => ...
    mi.x_type = "A Type"
    => "A Type"
    mi.x_type
    => "A Type"
    mi.identifier_type = "this type"
    => "this type"
    mi.identifier_type
    => nil
    
  • James B. Byrne

    James B. Byrne March 23rd, 2009 @ 08:20 PM

    • Tag changed from 2.3.2, activerecord, attributes, defect to invalid

    Please close. Invalid.

  • Matt Jones

    Matt Jones March 25th, 2009 @ 02:22 AM

    What was the final resolution? Before closing the ticket, it's good to know what happened, in case others run into the same problem.

  • James B. Byrne

    James B. Byrne April 30th, 2009 @ 08:22 PM

    Sorry for the late replay, I did not get an email notice when the ticket was updated, or missed it if I did.

    The error was mine. I had the attr_protected set for those fields in the model that I was testing.

  • Frederick Cheung

    Frederick Cheung May 7th, 2009 @ 11:20 PM

    • State changed from “new” to “invalid”

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

Tags

Pages