I'm not really a fan of the idea for having to specify :_create in the 
attributes for each nested model. That would make this feature quite cumbersome 
to use. Also, I feel that would confuse things as you can currently create 
nested models (albeit without a specific PK) without any special :_create 
attribute.

How about, as you initially suggested, if it just requires you to call 
.unrestrict_primary_key in the parent model class?

Josh 
Cheers

Josh
On Tuesday, 15 February 2011 at 10:03 AM, Jeremy Evans wrote: 
> On Feb 14, 2:52 pm, Josh Bassett <[email protected]> wrote:
> > I'm pretty sure I did.
> > 
> > In the nested_attributes.rb code I see the method responsible:
> > 
> > def nested_attributes_setter(reflection, attributes)
> > return if (b = reflection[:nested_attributes][:reject_if]) && 
> > b.call(attributes)
> > modified!
> > klass = reflection.associated_class
> > if pk = attributes.delete(klass.primary_key) || 
> > attributes.delete(klass.primary_key.to_s)
> > if klass.db.send(:typecast_value_boolean, attributes[:_delete] || 
> > attributes['_delete']) && reflection[:nested_attributes][:destroy]
> > nested_attributes_remove(reflection, pk, :destroy=>true)
> > elsif klass.db.send(:typecast_value_boolean, attributes[:_remove] || 
> > attributes['_remove']) && reflection[:nested_attributes][:remove]
> > nested_attributes_remove(reflection, pk)
> > else
> > nested_attributes_update(reflection, pk, attributes)
> > end
> > else
> > nested_attributes_create(reflection, attributes)
> > end
> > end
> > 
> > It appears that the block within the conditional:
> > 
> > if pk = attributes.delete(klass.primary_key) || 
> > attributes.delete(klass.primary_key.to_s)
> > 
> > will only perform a remove or update operation.
> 
> Your analysis is correct. I'm not sure this is a good candidate for
> supporting such code by default, though. Maybe there should be an
> easy to override method where you can pass in the reflection and
> attributes and it determines which method to run (create/update/remove/
> delete)? That would require some refactoring, but would allow you to
> easily get the behavior you want by overriding a simple method. Does
> that sound reasonable to you?
> 
> Jeremy
> 
> I'll accept a patch with specs that allows you to provide a option to
> the nested_attributes_method (:pk_create ?) where if you provide a
> _create key in the attributes, it will create an associated object
> instead of updating. That doesn't appear to be exactly what you want,
> though. You appear to want the plugin to automatically choose whether
> to create or update based on the current content in the database.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sequel-talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sequel-talk?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to