Is "target" a reserved word?


#1

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


#2

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


#3

I opened a ticket for this issue:
http://dev.rubyonrails.org/ticket/4769


Josh S.
http://blog.hasmanythrough.com


#4

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-part-2.html

–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


#5

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