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.

v 2.3.2 - appropriates attributes ending in _type and _value

#2314

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 => #

Reported by James B. Byrne · March 23rd, 2009 @ 05:27 PM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. James B. Byrne
    James B. Byrne

    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>
    >> 
    

    March 23rd, 2009 @ 05:31 PM

  2. James B. Byrne
    James B. Byrne
    • 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

    March 23rd, 2009 @ 05:32 PM

  3. James B. Byrne
    James B. Byrne
    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
    

    March 23rd, 2009 @ 05:36 PM

  4. James B. Byrne
    James B. Byrne

    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
    

    March 23rd, 2009 @ 07:44 PM

  5. James B. Byrne
    James B. Byrne
    • Tag changed from 2.3.2, activerecord, attributes, defect to invalid

    Please close. Invalid.

    March 23rd, 2009 @ 08:20 PM

  6. Matt Jones
    Matt Jones

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

    March 25th, 2009 @ 02:22 AM

  7. James B. Byrne
    James B. Byrne

    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.

    April 30th, 2009 @ 08:22 PM

  8. Frederick Cheung
    Frederick Cheung
    • State changed from new to invalid

    May 7th, 2009 @ 11:20 PM