[MacRuby-devel] Create new Core Data Instances

Sven A. Schmidt sas at abstracture.de
Mon Nov 14 00:47:23 PST 2011

Hi Timo,

> a) Is it really THAT complicated to create an instance of MyEntity and set the attribute? I'm coming from the Rails/ActiveRecord world where this could easily be done with a single line of code (including saving the instantiated object): MyEntity.new(:attribute1 => "Test").save )
> b) How can I save this fresh instance and update my NSTableView?

After you have modeled your entity in the Xcode core data modeling view, you can use

    instance = NSEntityDescription.insertNewObjectForEntityForName(
      "MyEntity", inManagedObjectContext:managedObjectContext

to instantiate an instance. You can save it calling

    error = Pointer.new_with_type("@")

To save attributes, simply assign to them:

    instance.attribute1 = 'Test'

This assumes you've added that attribute to your model. The managedObjectContext can be created or, if you're using a core data project like the document one, you'll have a managedObjectContext accessible from your NSDocument subclass: self. managedObjectContext

If you have bound your NSTableView to the model object, changes will be picked up automatically. You'll only need to call save to actually persist to disk.

I hope this helps somewhat, there's much more detail to it and the following links explain more of those:


and some MacRuby specific ones that I've found useful in the past



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3770 bytes
Desc: not available
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20111114/f6454556/attachment-0001.bin>

More information about the MacRuby-devel mailing list