Forum: Ruby on Rails Trying to come up with a nice DSL, but having some problems

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Pat M. (Guest)
on 2006-04-11 05:02
(Received via mailing list)
I have three models in this small game I'm working on - the Game,
Players, and Turns.  A Player should be able to rescue another Player
on any given turn.  So it looks like this:

class Game
  has_many :players
  has_many :turns

  def rescue_player(p)
    t = turns.last
    t.rescued = p

class Turn
  belongs_to :rescued, :foreign_key => 'rescued_id', :class_name =>

class Player
  belongs_to :game

 def rescue_player(p)
    game.rescue_player p

So what I want to do is be able to create a game object and do
something like this:

g = create_game # factory method in test_helper that just creates a
new random game
g.players.first.rescue_player g.players.last
assert_equal g.players.last, g.turns.last.rescued

Now if I just do that, the data gets saved in the database, but the
assert fails.  This is because g !=  I can do a
g.reload right before the assert and then it works fine.  I'm just not
quite sure what I need to do, because I think it's pretty lame to have
to reload every time I do something like this.  Also when I'm actually
using the code I won't remember to call reload, as it's just not
really a natural thing to do.  Can anyone give me some advice on how
to create the DSL I want, without resorting to adding ugliness like
random reload calls?

This topic is locked and can not be replied to.