MotionModel icon indicating copy to clipboard operation
MotionModel copied to clipboard

Model relation question

Open kapso opened this issue 11 years ago • 1 comments

Given the following models/relationship -

class Address
  include MotionModel::Model
  include MotionModel::ArrayModelAdapter

  columns street: :string, city: :string

  belongs_to :user
end

class User
  include MotionModel::Model
  include MotionModel::ArrayModelAdapter

  columns name: :string

  has_one :address
end

And this user hash:

{ "id"=>1, "name"=>"Steve", "address"=> { "id"=>1, "street"=>"2261 Market Street", "city"=>"San Francisco" } }

When I do the following User.new(user_hash).save the Address.count is still 0 ??

kapso avatar Jan 19 '14 08:01 kapso

We've heard about this use-case before but it didn't seem like a core MotionModel issue because it's a deserialization from external source task. The best way to accomplish this is to parse and stuff the data yourself. You may want to look at the Formotion code to see how creating objects from hashes is accomplished.

You are better able to know the relationships in your own code that MotionModel is at automagically inferring it.

If you come up with a generalized solution (maybe a recursion), that would be a really cool addition to MotionModel.

Thanks

sxross avatar Jan 19 '14 18:01 sxross