Forum: Ruby on Rails Mass Assginment and has_many others.build

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-03-23 17:57
Typically, when one creates a has_many associated table one does this:

hasone.hasmany.build({attribute hash},{attribute hash},..} or
hasone.hasmany.create({attribute hash}, ...

However, if mass assignment is turned off (attr_accessible => nil), then
I cannot seem to set the attributes directly.

For example, in a console session I do this:

>> me = Entity.new
=> ...
>> me.attribute = ...
...
>> me.save!
=> true
>> mi = me.identifiers.build
=> ...
>> mi.entity_id
=> 1
>> mi.identifier_type = 'post'
=> 'post'
>> mi.identifier_type
=> nil

What is going on and how do I directly set association attributes?
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-03-23 18:49
Rails 2.3.2

This seems related specifically to the suffices _type and _value.
Attributes ending in either of these strings are not processed by the
attribute= method.  if #write_attribute("something_type", "a value") is
called instead, then the somthing-type= method is again bypassed and the
attribute is set to the literal value provided instead.

I have raised a ticket for this:

http://rails.lighthouseapp.com/projects/8994/ticke...
This topic is locked and can not be replied to.