Forum: Ruby on Rails Is "target" a reserved word?

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.
Josh on Rails (Guest)
on 2006-04-17 20:28
(Received via mailing list)
I'm working on a cost estimation tool, that allows each estimate created
to
override base assumptions of price, cost, etc.

Everything was fine, until we said, "Let's add a 'target net revenue'
property."

So, the resource model got a "target" column. The override model got a
"new_target" column.

But, whenever I set resource.target, I don't get a resource at the end;
I
get target. In other words:

old_resource = resource.clone
resource.target = new_target
resource == old_resource # false
resource == new_target # true
Josh S. (Guest)
on 2006-04-17 21:54
Josh on Rails wrote:
> I'm working on a cost estimation tool, that allows each estimate created
> to override base assumptions of price, cost, etc.
>
> Everything was fine, until we said, "Let's add a 'target net revenue' property."
>
> So, the resource model got a "target" column. The override model got a
> "new_target" column.
>
> But, whenever I set resource.target, I don't get a resource at the end;
> I get target. In other words:
>
> old_resource = resource.clone
> resource.target = new_target
> resource == old_resource # false
> resource == new_target # true

"target" is a method on AssociationProxy, which is the parent of the
class that implements the "resource" association attribute on your
model. resource.target is the associated object itself, not its resource
attribute! I spent part of the weekend delving into that code, and I
think the proxy methods should all have names that start with "proxy_"
to avoid those sorts of collisions.  Or at least be documented!

--
Josh S.
http://blog.hasmanythrough.com
Josh S. (Guest)
on 2006-04-18 00:44
I opened a ticket for this issue:
http://dev.rubyonrails.org/ticket/4769

--
Josh S.
http://blog.hasmanythrough.com
Brendan Baldwin (Guest)
on 2007-06-20 03:20
I feel your pain.  I just posted about this after hitting the same issue
at work today.  I must have spent a few hours trying to figure out what
was wrong with our model, because in our case 'target' was an
association itself, which meant that the consequences of using it were
much stealthier and weirder:

http://www.rubyfu.com/2007/06/transparent-proxies-...

--Brendan

Josh on Rails wrote:
> I'm working on a cost estimation tool, that allows each estimate created
> to
> override base assumptions of price, cost, etc.
>
> Everything was fine, until we said, "Let's add a 'target net revenue'
> property."
>
> So, the resource model got a "target" column. The override model got a
> "new_target" column.
>
> But, whenever I set resource.target, I don't get a resource at the end;
> I
> get target. In other words:
>
> old_resource = resource.clone
> resource.target = new_target
> resource == old_resource # false
> resource == new_target # true
Shawn A. (Guest)
on 2007-06-20 20:01
(Received via mailing list)
target is not a reserved word.  It is however used in ActiveRecord for
their
associations.  That might be where you are running into problems.

/Shawn
This topic is locked and can not be replied to.