[Rails] accepts nest attributes & attr_accessible
Hey,
Just going over some of the accepts of accepts_nested_attributes_for
& attr_accessible.
I found with the form and models I've been working on it would be
great if I could use accepts_nested_attribtues but I want to propose
my case where I just can't make it work. And for secretory reasons
wonder where it would ever work for that matter.
So below. We have a household setup. We would have a Family object
with an id. Now if we were to create one person for the family we
could do it easily with:
@family = Family.new
@family.persons.new(params[person])
But what if we wanted to add a person and a child at the same time via
nested parameters? We won't be able to. As the childs family_id field
is protected we won't be able to mass assign with the nested
parameters. I can't think of a time I would ever be creating a
completely "open" object that wouldn't have a single protected
parameter. So when do the nested_attributes come in handy besides when
your model is un-secure?
(This is just me learning there very well might be perfect places for
this, I am just wondering where)
class Person < ActiveRecord::Base
validates_presence_of :name, :family_id
has_many :children
belongs_to :family
accepts_nested_attributes_for :children
attr_accessible :name
end
class Child < ActiveRecord::Base
belongs_to :person
belongs_to :family
validates_presence_of :name, :family_id
attr_accessible :name
end
class Family < ActiveRecord::Base
has_many :persons
has_many :children (childs?)
attr_protected :id
end
cheers,
brianp
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home