I am curious as to the best practices for the following use case:

You have two models, Parent and Kid.

# parent model
has 0..n, :kids

# kid model
belongs_to :parent

I would like to be able to do the following:

@parent.first
@parent.kids.update_or_create(kid_hash)

# in the Kid model
def self.update_or_create(kid_hash)
  if kid = first(kid_hash)
    kid.update(kid_hash)  #pseudo-code
  else
    kid = create(kid_hash)
  end
end

The problem with the above is that the method has no idea that it is
"scoped" by the @parent and thus does not know the "parent_id", nor
could I find a way to determine this.

I have successfully tried two alternate ways of doing this:
1 - with a method in the parent model "update_or_create_kid" and do
the same operations from the Parent model.  This logic seems like it
should belong in the Kid model though.

2 - by passing the @parent.id inside of the kid_hash.
@parent.first
kid_hash[:parent_id] = @parent.id
Kid.update_or_create(kid_hash)

Am I missing something to get this working?  Is there an alternate way
to accomplish this?  Or is one of the alternate ways the best way?

Thanks,
- Mark
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to