hasOne/hasMany - where is the reference injected?

28 views
Skip to first unread message

Sorin Carbunaru

unread,
Jun 24, 2014, 8:29:28 AM6/24/14
to ged...@googlegroups.com
I have a unidirectional one-to-many relationship between Game and Highscore, and I want Highscore to contain a reference to a Game.

How do I do this?

Game
this.hasMany(Highscores);

OR

Highscore
this.hasOne(Game);

Again, I want Highscore to have the reference.

Ben Ng

unread,
Jun 24, 2014, 1:10:45 PM6/24/14
to ged...@googlegroups.com
I believe you want highscore belongsTo game.

Ben
--
The official community discussion group.
website: geddyjs.org, source: https://github.com/mde/geddy, group: https://groups.google.com/d/forum/geddyjs?hl=en
---
You received this message because you are subscribed to the Google Groups "GeddyJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geddyjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sorin Carbunaru

unread,
Jun 24, 2014, 1:32:38 PM6/24/14
to
I've read belongsTo is just the inverse of hasOne/hasMany, and it is not even required. 

Ben Ng

unread,
Jun 24, 2014, 1:38:07 PM6/24/14
to ged...@googlegroups.com
I have a comment belongsTo story in my app and can confirm that story_id is in the comment table.

I’m not sure what behavior is going to be if you don’t specify belongsTo. I would expect eager loading something like comment.story to fail if belongsTo wasn’t specified.

Ben

On June 24, 2014 at 10:32:40 AM, 'Sorin Carbunaru' via GeddyJS (ged...@googlegroups.com) wrote:

I've read belongsTo is just the inverse of hasOne/hasMany, and it is not even required. 

Matthew Eernisse

unread,
Jun 24, 2014, 1:39:40 PM6/24/14
to ged...@googlegroups.com
Ben is right. hasMany and belongsTo can use the same foreign key, but until we implement the 'inverse' feature, they don't know anything about each other.

Sorin Carbunaru

unread,
Jun 24, 2014, 1:51:21 PM6/24/14
to ged...@googlegroups.com
Ok. So having (highscore belongsTo game) and (highscore belongsTo player) will make highscore to have references to both game and player, right? 

Matthew Eernisse

unread,
Jun 24, 2014, 1:53:55 PM6/24/14
to ged...@googlegroups.com
Yes, that will create a game_id and player_id on highscore. Are you doing a World Cup API? :)


On Tue, Jun 24, 2014 at 10:51 AM, 'Sorin Carbunaru' via GeddyJS <ged...@googlegroups.com> wrote:
Ok. So having (highscore belongsTo game) and (highscore belongsTo player) will make highscore to have references to both game and player, right? 

--

Sorin Carbunaru

unread,
Jun 24, 2014, 1:59:26 PM6/24/14
to ged...@googlegroups.com
:)). No, not a World Cup API. I'm working on my Master's thesis and I'm generating some Geddy code from a diagram. It's about model-driven engineering.

Matthew Eernisse

unread,
Jun 24, 2014, 2:03:15 PM6/24/14
to ged...@googlegroups.com
Ah, I see. :) Sounds awesome. Let us know if there's anything else we can do to help.


On Tue, Jun 24, 2014 at 10:59 AM, 'Sorin Carbunaru' via GeddyJS <ged...@googlegroups.com> wrote:
:)). No, not a World Cup API. I'm working on my Master's thesis and I'm generating some Geddy code from a diagram. It's about model-driven engineering.

--

Sorin Carbunaru

unread,
Jun 24, 2014, 2:16:07 PM6/24/14
to ged...@googlegroups.com
Thank you for your availability. 
Reply all
Reply to author
Forward
0 new messages