sxross / MotionModel

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

undefined method `include?' for #<MotionModel:: ...> #129

Closed tomoyuki28jp closed 9 years ago

tomoyuki28jp commented 9 years ago

Error

(main)> 2014-11-26 09:19:32.434 App[10605:55297] model.rb:739:in `block in push_has_many_attr:': undefined method `include?' for #<MotionModel::ArrayFinderQuery:0x131f0340 ...> (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:19:32.436 App[10605:55297] model.rb:739:in `block in push_has_many_attr:': undefined method `include?' for #<MotionModel::ArrayFinderQuery:0x131f0340 ...> (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:19:32.486 App[10605:55297] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'model.rb:739:in `block in push_has_many_attr:': undefined method `include?' for #<MotionModel::ArrayFinderQuery:0x131f0340 ...> (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:'

Gemfile

source 'https://rubygems.org'

gem 'rake'
gem 'bubble-wrap', '~> 1.7.1'
gem 'formotion'
gem 'motion-my_env'
gem 'cocoapods'
gem 'motion-cocoapods', github: 'HipByte/motion-cocoapods'
gem 'motion-kit'
gem 'motion-support'
gem 'afmotion'
gem 'motion-layout'
gem 'sugarcube', require: ['sugarcube-color']
gem 'motion_model'

Gemfile.lock

GIT
  remote: git://github.com/HipByte/motion-cocoapods.git
  revision: e77482eb3a8cd5dffa9e5314853d21177c10fc14
  specs:
    motion-cocoapods (1.6.0)
      cocoapods (>= 0.34)

GEM
  remote: https://rubygems.org/
  specs:
    activesupport (3.2.19)
      i18n (~> 0.6, >= 0.6.4)
      multi_json (~> 1.0)
    afmotion (2.4.1)
      motion-cocoapods (>= 1.4.1)
      motion-require (>= 0.1)
    bubble-wrap (1.7.1)
      bubble-wrap-http (= 1.7.1)
    bubble-wrap-http (1.7.1)
    claide (0.7.0)
    cocoapods (0.34.4)
      activesupport (>= 3.2.15, < 4)
      claide (~> 0.7.0)
      cocoapods-core (= 0.34.4)
      cocoapods-downloader (~> 0.7.2)
      cocoapods-plugins (~> 0.3.1)
      cocoapods-trunk (~> 0.3.1)
      cocoapods-try (~> 0.4.1)
      colored (~> 1.2)
      escape (~> 0.0.4)
      json_pure (~> 1.8)
      nap (~> 0.8)
      open4 (~> 1.3)
      xcodeproj (~> 0.19.4)
    cocoapods-core (0.34.4)
      activesupport (>= 3.2.15)
      fuzzy_match (~> 2.0.4)
      json_pure (~> 1.8)
      nap (~> 0.8.0)
    cocoapods-downloader (0.7.2)
    cocoapods-plugins (0.3.1)
      nap
    cocoapods-trunk (0.3.1)
      json_pure (~> 1.8)
      nap (>= 0.8)
      netrc (= 0.7.8)
    cocoapods-try (0.4.1)
    colored (1.2)
    dbt (1.1.5)
    escape (0.0.4)
    formotion (1.8)
      bubble-wrap (>= 1.4.0)
      motion-require (>= 0.1.0)
    fuzzy_match (2.0.4)
    i18n (0.6.11)
    json_pure (1.8.1)
    motion-kit (0.14.0)
      dbt (~> 1.1.5)
    motion-layout (0.0.2)
    motion-my_env (0.2)
    motion-require (0.2.0)
    motion-support (0.2.6)
      motion-require (>= 0.0.6)
    motion_model (0.6.0)
      bubble-wrap (>= 1.3.0)
      motion-support (>= 0.1.0)
    multi_json (1.10.1)
    nap (0.8.0)
    netrc (0.7.8)
    open4 (1.3.4)
    rake (10.3.2)
    sugarcube (2.11.1)
    xcodeproj (0.19.4)
      activesupport (~> 3.0)
      colored (~> 1.2)

PLATFORMS
  ruby

DEPENDENCIES
  afmotion
  bubble-wrap (~> 1.7.1)
  cocoapods
  formotion
  motion-cocoapods!
  motion-kit
  motion-layout
  motion-my_env
  motion-support
  motion_model
  rake
  sugarcube
tomoyuki28jp commented 9 years ago

I think this error occurs when I use has_many + belongs_to.

tomoyuki28jp commented 9 years ago

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:'
sxross commented 9 years ago

Can you repro this with a spec or small project?

tomoyuki28jp commented 9 years ago

@sxross Thanks for your reply. I've created a small app to reproduce this error. https://github.com/tomoyuki28jp/MotionModelError

tomoyuki28jp commented 9 years ago

@sxross The error disappears when I made following change.

-  has_many :photos
+  has_many :photo

[UPDATE] But this is not just working...

sxross commented 9 years ago

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.

tomoyuki28jp commented 9 years ago

@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 :)