So AR has the feature where you can create records using the
association:
game = Game.find(1)
game.game_players.create(opts)
While it is "kind of" just sugar for:
game = Game.find(1)
GamePlayer.create(opts.merge(:game_id => game.id))
I have definitely observed programmers making fewer mistakes on the
former method. For one thing, if you're doing lots of dependent
records, it's easy to accidentally put the wrong id there and totally
miss it visually:
game = Game.find(1)
GamePlayer.create(opts.merge(:game_id => game_stats.id))
This likely won't trigger an error since odds are there will be a
record with the same sequence, so the FK will work. Even spec'ing
won't catch is since you'll probably be mocking everything with ID's
1,2,3,4,5 etc.
Whereas, this is a bit more obvious something's not right:
game = Game.find(1)
game_stats.game_player.create(opts)
In Sequel obviously this syntax doesn't work, because
game.game_players is an Array (rather than a special Collection like
in AR that has extra methods)
I'm not at all tied to this specific syntax per se. Is there an
equivalent in Sequel, maybe using subset or similar scoping?
BTW I'll be glad to help summarize these posts and help contribute to
the Sequel/AR FAQ.
Thanks,
Nate
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---