sxross / MotionModel

Simple Model and Validation Mixins for RubyMotion
MIT License
192 stars 67 forks source link

Model relation question #103

Open kapso opened 10 years ago

kapso commented 10 years ago

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 ??

sxross commented 10 years ago

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