motion_data_wrapper icon indicating copy to clipboard operation
motion_data_wrapper copied to clipboard

can't find .association<< method

Open crosebrugh opened this issue 11 years ago • 8 comments

I've got to assume I'm having n00b issues (since I can't imagine this not working) so patience will be appreciated.

data models:

Business has_many Sites

Site belongs_to Business

% Business.current.sites 
=> NSSet(0){}

% Business.current.sites << Site.new 
=> undefined method `<<' for NSSet(0){}:_NSFaultingMutableSet (NoMethodError)

My associations are setup exactly like this example: https://github.com/sxross/motion_data_wrapper-example

Any idea why the NSSet is returned instead of an array? More importantly, why no 'sites<<' method? I don't see any such method being created in your code.

A nit: in relation/core_data.rb you have:

      def context
        @ctx || App.delegate.managedObjectContext
      end

seems it should be:

      def context
        @ctx ||= App.delegate.managedObjectContext
      end

crosebrugh avatar Nov 12 '13 03:11 crosebrugh

By default, CoreData returns an NSSet and not an NSArray when you are accessing a has_many relationship. Read this SO article about it: http://stackoverflow.com/a/17018816

I think in your case, you should be doing:

bus = Business.current
bus.addSiteObject Site.new

Optionally, you could create an NSMutableSet from the NSSet and assign that back to the property by hand:

bus = Business.current
sites = NSMutableSet.setWithSet(bus.sites)
sites.addObject Site.new
bus.sites = sites

macfanatic avatar Nov 12 '13 11:11 macfanatic

Thanks for the quick response and SO pointer.

My confusion is due to this example in your README:

matt = Person.new
matt.addresses << Address.new(street: "123 Lane")
matt.addresses << Address.new(country: "USA")
matt.save!

Why does '<<' work here?

crosebrugh avatar Nov 12 '13 16:11 crosebrugh

Either that worked at some point and was documented, but doesn't now, or more likely never worked and was just documented incorrectly :(

Sorry about the confusion, I'll turn this ticket into a PR to update the README to reflect our discussion.

macfanatic avatar Nov 12 '13 17:11 macfanatic

Any way we can get the readme updated with a more realistic relational example? This is tripping me up too.

trying to do something like:

item = WishlistItem.new(item:1234, qtyFree:1) # qtyFree is the only required attribute for WishListItem
Hostesses.shared_hostess.current_hostess.wishlist << item

and getting #<NoMethodError: undefined method '<<' for #<_NSFaultingMutableSet:0xd5da080>>

Here's my data model:

screenshot 2014-05-15 20 16 50

markrickert avatar May 16 '14 00:05 markrickert

Oh, i also tried doing something like this as well:

Hostesses.shared_hostess.current_hostess.addWishlistObject(item)
=> #<NSException:0x178b3f10>

markrickert avatar May 16 '14 00:05 markrickert

OK, i think I'm getting somewhere... sorry for all the messages. When I do the above, i get an NSException returned to me, but then when I do:

(main)> Hostesses.shared_hostess.current_hostess.wishlist.allObjects
=> [#<WishlistItem catalog: nil, item: 1234, name: nil, pages: nil, price: 0.0, qtyFree: 1, qtyHalfPrice: 0, retired: nil, stopSell: nil, type: nil>]

it seems like the item was inserted into the wishlist attribute.

Running Hostesses.shared_hostess.current_hostess.wishlist gives me #<_NSFaultingMutableSet:0x109d5b10> for some reason.

When I run Hostesses.shared_hostess.current_hostess.save i get a big fat error message:

2014-05-15 20:29:52.726 Closer[58755:70b] persistence.rb:92:in `save!': wishlist invalid (MotionDataWrapper::RecordNotSaved)
    from persistence.rb:75:in `save'
=> false

any help would be appreciated!

markrickert avatar May 16 '14 00:05 markrickert

Looking over your posts, I'm honestly not sure what's going on. With the example core data schema shown above, I would think the following would work:

h = Hostess.new
w = WishlistItem.new

h.addWishlistObject w
h.save

The fact that you get a NSFaultingMutableSet returned isn't a big deal from what I remember - believe that is CoreData telling you it hasn't actually retrieved the records from the store yet and it behaves as a proxy object very similar to AR::Relation in Rails.

Can you post your validations you have configured in CD? The fact that save! raises an exception definitely looks like something isn't happy here. You can also try h.errors to get a printout from REPL.

macfanatic avatar May 19 '14 12:05 macfanatic

FYI - over the weekend I tried out CDQ and ruby-xcdm and I think I may go that way instead. Still trying to weigh my options here. I really like the auto-generation of the xcdatamodel file with ruby-xcdm.

markrickert avatar May 19 '14 15:05 markrickert