# Object isn't being saved when called through association

**URL:** <https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789>\
**Category:** Rails\
**Created:** [April 10, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789 "2006-04-10T14:14:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789/1 "2006-04-10T14:14:34Z")

</div>

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§  
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§  
game.rescue\_player p  
end  
end

What I’m finding is that if I take a player and do  
player.rescue\_player p

Then it doesn’t actually save the results from the game#rescue\_player  
method. But if I call game.rescue\_player§ directly, it does. I  
have no clue what’s up, and it’s confusing the hell out of me. Am I  
not allowed to access it like this or something?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789/2 "2006-04-10T14:39:37Z")

</div>

One thing I’ve found is that the game object in Player#rescue\_player  
is not the same as the game object in Game#rescue\_player. Here’s some  
output from a test:

game is #Game:0x245c36c  
player’s game is #Game:0x24471c4  
game id is 1  
player’s game id is 1

I think the objects ought to be the same, as the Player is related to  
the Game…what’s going on?

One last thing, I just tried reloading the game object after calling  
player.rescue\_player, and it reflected the rescued player…but that  
seems like a pretty ugly hack really. What am I doing wrong?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 15, 2006, 12:16am UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789/3 "2006-04-15T00:16:23Z")

</div>

On 4/14/06, Justin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I think the objects ought to be the same, as the Player is related to  
> > regards
> 
> Justin
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Yeah, that’s what I found…so is there any way I can use create the  
DSL I want without resorting to lots of reload calls?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 15, 2006, 2:21am UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789/4 "2006-04-15T02:21:34Z")

</div>

Pat M. wrote:

> On 4/14/06, Justin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Active Record doesn’t give you a unique in-memory representation of a  
> > given database record. If you retrieve the same record more than once  
> > you will get multiple instances representing it.
> 
> Yeah, that’s what I found…so is there any way I can use create the  
> DSL I want without resorting to lots of reload calls?

By DSL do you just mean “domain model”?

I suggest that you code your application based on an understanding of  
the limitations of Active Record, and refactor out redundancy. You might  
find that you end up with generic code ensuring unique representation of  
each entity within a user’s session, or you might find that there are  
only a few places where you need to guard against duplication.

regards

Justin

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 14, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789/5 "2006-04-14T21:11:21Z")

</div>

Pat M. wrote:

> the Game…what’s going on?
> 
> One last thing, I just tried reloading the game object after calling  
> player.rescue\_player, and it reflected the rescued player…but that  
> seems like a pretty ugly hack really. What am I doing wrong?

Active Record doesn’t give you a unique in-memory representation of a  
given database record. If you retrieve the same record more than once  
you will get multiple instances representing it.

regards

Justin

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 15, 2006, 2:30am UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789/6 "2006-04-15T02:30:37Z")

</div>

On 4/14/06, Justin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> By DSL do you just mean “domain model”?  
> I mean domain specific language.

One player should be able to rescue another player within the game.  
Thus it would be really slick to be able to write  
player.rescue another\_player

That does modify the database as I need to, but it makes testing, as  
well as some controller stuff, quite messy. For example, I want to  
display the game details on the page, and rescue the player.

def rescue\_player  
@game = … # get’s the game based on some session info  
player = … # again get’s player info based on session  
another\_player = Player.find params[:id]  
player.rescue another\_player  
@game.reload  
end

If I don’t do @game.reload, the @game object won’t accurately reflect  
the db. First of all I just think it’s ugly to have to do that, and  
secondly it’s pretty easy to forget the reload, not know when I need  
to call it, etc.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 15, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/object-isnt-being-saved-when-called-through-association/93789/7 "2006-04-15T12:06:40Z")

</div>

Pat M. wrote:

> I mean domain specific language.
> 
> One player should be able to rescue another player within the game.  
> Thus it would be really slick to be able to write  
> player.rescue another\_player

OK. To me, that just looks like a feature of the domain model -  
straightforward OO, with no metaprogramming required. (Contrast this  
with the DSL for associations in Rails: has\_many, belongs\_to, etc.)

> @game.reload  
> end
> 
> If I don’t do @game.reload, the @game object won’t accurately reflect  
> the db. First of all I just think it’s ugly to have to do that, and  
> secondly it’s pretty easy to forget the reload, not know when I need  
> to call it, etc.

From what you say, the game already contains information about the  
players, so if you go to the database without involving the game, and  
change anything, the game becomes out of date.

Perhaps you should do all finding of players (etc.) via the game, and  
give it responsibility for holding these things uniquely.

regards

Justin
