jagi / meteor-astronomy

Model layer for Meteor
https://atmospherejs.com/jagi/astronomy
MIT License
605 stars 66 forks source link

I'm having difficulty figuring out how to use "Relations" #215

Closed crussi closed 8 years ago

crussi commented 8 years ago

Hi Jagi, First off, you're work is AMAZING! You're so talented and I've been really enjoying working with Astronomy. Everything is going well and I'm in awe of your work. I'm having difficulty creating a relations. I've followed the documentation to the letter. I've read and re-read the documentation and even the source code. For some reason, my relations is always Null. I tried a very simple relation. Here is a small snippet: DelegateId: { type: 'string', default:'' }, Delegate: { type: 'string', default:'', transient: true }, relations: { delegate: { type: 'one', class: 'Contact', local: 'DelegateId', foreign: '_id' } }, My class Task has a DelegateId that relates to a Contact table's _id field. It can't get any easier. Any ideas what I might be doing wrong?

lukejagodzinski commented 8 years ago

Please use code formatting in the future. I will update documentation with information about creating one to one relation. And here is a solution for your problem:

Contacts = new Mongo.Collection('contacts');

Contact = Astro.Class({
  name: 'Contact',
  collection: Contacts,
  fields: {
    taskId: 'string'
  }
});

Tasks = new Mongo.Collection('tasks');

Task = Astro.Class({
  name: 'Task',
  collection: Tasks,
  fields: {
    name: 'string'
  },
  relations: {
    contact: {
      type: 'one',
      class: 'Contact',
      local: '_id',
      foreign: 'taskId'
    }
  }
});

var task = new Task({name: 'Name'});
task.save();
var contact = new Contact({taskId: task._id});
contact.save();

// Now to retrieve contact you can type:
task.contact();
crussi commented 8 years ago

Hi Jagi, Thank you for your response! What you're suggesting is exactly what I tried. I apologize for not formatting code, I normally do, but, I was in a hurry. One thing I failed to mention is that I am using React inside Meteor. I will set up a test regular Meteor project today and test your code example. I must be doing something wrong and I just don't know it. I found a work-a-round that kept me working forward. I will document my idea to see if it is acceptable within Astronomy. But, basically, I put a method inside my class that is called afterInit. It loads the subdocument and this seems to work fine. I haven't tested it in all situations. I have to say again that your work is amazing and you're very talented. Astronomy is the holy grail for Meteor. Chris

On Thu, Dec 10, 2015 at 2:24 PM, jagi notifications@github.com wrote:

Please use code formatting in the future. I will update documentation with information about creating one to one relation. And here is a solution for your problem:

Contacts = new Mongo.Collection('contacts');

Contact = Astro.Class({ name: 'Contact', collection: Contacts, fields: { taskId: 'string' } });

Tasks = new Mongo.Collection('tasks');

Task = Astro.Class({ name: 'Task', collection: Tasks, fields: { name: 'string' }, relations: { contact: { type: 'one', class: 'Contact', local: '_id', foreign: 'taskId' } } }); var task = new Task({name: 'Name'});task.save();var contact = new Contact({taskId: task._id});contact.save(); // Now to retrieve contact you can type:task.contact();

— Reply to this email directly or view it on GitHub https://github.com/jagi/meteor-astronomy/issues/215#issuecomment-163766943 .

lukejagodzinski commented 8 years ago

Oh sorry I've missed your response. I think the best way of solving your problem (if you still have it) will be when you provide some reproduction application and I will tell you where is a bug.