Closed tomoyuki28jp closed 9 years ago
I think this error occurs when I use has_many + belongs_to.
I've also tried gem 'motion_model', github: 'sxross/MotionModel'
, but I get the same error.
(main)> 2014-11-26 09:36:30.043 App[13759:66804] model.rb:739:in `block in push_has_many_attr:': undefined method `include?' for #<MotionModel::ArrayFinderQuery:0x169089e0 ...> (NoMethodError)
from model.rb:738:in `push_has_many_attr:'
from model.rb:750:in `set_has_many_attr:'
from model.rb:369:in `block in define_has_many_methods:'
from model.rb:679:in `set_attr:'
from model.rb:481:in `block in attributes=:'
from model.rb:481:in `attributes=:'
from model.rb:859:in `initialize_data_columns:'
from model.rb:433:in `block in initialize:'
from model.rb:432:in `initialize:'
from model.rb:63:in `block in new:'
from model.rb:62:in `new:'
from model.rb:233:in `create:'
from posts_controller.rb:50:in `tableView:heightForRowAtIndexPath:'
from posts_controller.rb:31:in `block in viewDidLoad'
from query.rb:399:in `call_delegator_with_response'
from query.rb:144:in `connectionDidFinishLoading:'
2014-11-26 09:36:30.045 App[13759:66804] model.rb:739:in `block in push_has_many_attr:': undefined method `include?' for #<MotionModel::ArrayFinderQuery:0x169089e0 ...> (NoMethodError)
from model.rb:738:in `push_has_many_attr:'
from model.rb:750:in `set_has_many_attr:'
from model.rb:369:in `block in define_has_many_methods:'
from model.rb:679:in `set_attr:'
from model.rb:481:in `block in attributes=:'
from model.rb:481:in `attributes=:'
from model.rb:859:in `initialize_data_columns:'
from model.rb:433:in `block in initialize:'
from model.rb:432:in `initialize:'
from model.rb:63:in `block in new:'
from model.rb:62:in `new:'
from model.rb:233:in `create:'
from posts_controller.rb:50:in `tableView:heightForRowAtIndexPath:'
from posts_controller.rb:31:in `block in viewDidLoad'
from query.rb:399:in `call_delegator_with_response'
from query.rb:144:in `connectionDidFinishLoading:'
2014-11-26 09:36:30.100 App[13759:66804] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'model.rb:739:in `block in push_has_many_attr:': undefined method `include?' for #<MotionModel::ArrayFinderQuery:0x169089e0 ...> (NoMethodError)
from model.rb:738:in `push_has_many_attr:'
from model.rb:750:in `set_has_many_attr:'
from model.rb:369:in `block in define_has_many_methods:'
from model.rb:679:in `set_attr:'
from model.rb:481:in `block in attributes=:'
from model.rb:481:in `attributes=:'
from model.rb:859:in `initialize_data_columns:'
from model.rb:433:in `block in initialize:'
from model.rb:432:in `initialize:'
from model.rb:63:in `block in new:'
from model.rb:62:in `new:'
from model.rb:233:in `create:'
from posts_controller.rb:50:in `tableView:heightForRowAtIndexPath:'
from posts_controller.rb:31:in `block in viewDidLoad'
from query.rb:399:in `call_delegator_with_response'
from query.rb:144:in `connectionDidFinishLoading:'
Can you repro this with a spec or small project?
@sxross Thanks for your reply. I've created a small app to reproduce this error. https://github.com/tomoyuki28jp/MotionModelError
@sxross The error disappears when I made following change.
- has_many :photos
+ has_many :photo
[UPDATE] But this is not just working...
Actually, this appears not to be a bug in MotionModel. You are trying to initialize a relation (called an ArrayFinderQuery
in this adapter) with an array. The way to do this is not by using an array but by creating the related object and adding it to the collection:
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
post = Post.create
photo = Photo.create
post.photos << photo
# Post.create({ :id => 1, :photos => [{ :post_id => 1 }] }) <= will fail because Array <> ArrayFinderQuery
true
end
end
Basically, what I'm saying is you can't initialize directly to a relation as though it was a scalar property. It's tempting to think that because you can use MotionModel's syntactic sugar to write:
post.photos << photo
you can also directly initialize from a nested hash. Sadly not.
On an sort of related note, I'm not sure why you are keeping track of the id's. Is the data from a remote source? MotionModel will take care of that for you.
If I have satisfied you that this was intended behavior in MotionModel, feel free to close.
@sxross Thank you for your time to take a look at my issue and explanation. I understand it so I close this issue! I appreciate your help. Have a good night :)
Error
Gemfile
Gemfile.lock