Ruby on Rails Sunday, October 2, 2011

(Frenchy, sorry =) )

Hi everyone, i've got some troubles with aggregated attribute.

I created a point and path class which receive postgresql point and
path type data. Path object contains an array of point object.

I'm currently working on a "hunting" module in web browser based game,
and i need a crontab executed every n minutes to move the character on
the map. I've created a function which remove the first point from the
path and then save:

def one_step_forward!
path_will_change!
path.points.shift
end

But look at this rails console tests:

ruby-1.9.3-rc1 :001 > @hunting = Hunting.find(1)
Hunting Load (7.6ms) SELECT "huntings".* FROM "huntings" WHERE
"huntings"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Hunting id: 1, path: "((7,6),(5,4))", created_at: "2011-10-02
16:02:11", updated_at: "2011-10-02 18:08:45">

ruby-1.9.3-rc1 :006 > @hunting.path
=> #<Path:0xaabba1c @points=[#<Point:0xaabb968 @x=7, @y=6>, #<Point:
0xaabb8c8 @x=5, @y=4>]>

ruby-1.9.3-rc1 :002 > @hunting.one_step_forward!
=> #<Point:0xaebc058 @x=7, @y=6>

ruby-1.9.3-rc1 :003 > @hunting.changed
=> ["path"]

ruby-1.9.3-rc1 :004 > @hunting.changes
=> {"path"=>[#<Path:0xaebbd10 @points=[#<Point:0xaebbe8c @x=5,
@y=4>]>, #<Path:0xa189da4 @points=[#<Point:0xaebbe8c @x=5, @y=4>]>]}

As you can seen, the attribute "path" is changed, but the path_was
return me the new path value! So when i'm using the save method,
nothing is saved...

I tried few things, like @path_was = path before the path_will_change!

Thank you in advance for any help.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment