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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-04-11 03: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
    t.save
  end
end

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

class Player
  belongs_to :game

 def rescue_player(p)
    game.rescue_player p
  end
end


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 != g.players.last.game.  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?

Pat
This topic is locked and can not be replied to.