I have two ActiveRecord classes, Player and User. Both underlying
tables have a column called email.
class User < ActiveRecord::Base
end
class Player < ActiveRecord::Base
belongs_to :user
end
If the player instance's user attribute is not nil I want any reference
to player.email get or set player.user.email - so I tried this...
class Player < ActiveRecord::Base
belongs_to :user
def email
return user.email if !(user.nil? || user.empty?)
end
end
... but it doesn't work. When I retreive a relevant player object and
try player.email I still get the email address from the player rather
than the user.
I can reproduce the same behaviour in console by doing
p = Player.find 4
p.email => "user-email@dot.com" # what i want
p[:email] => "player-email@dot.com" # not what i want
So are forms populated using second approach?
If so this seems to negate accessor overloading as far as form handling
is concerned.
Martin
> Zack,
>
> I added...
>
> def email
> user ? user.email : self[:email]
> end
>
> def email=(email)
> (user ? user : self).email = email
> end
>
> This still doesn't work, it still brings back the player.email value.
>
> Strangely it does work in console.
>
> Martin
>
>
>
> > > Hi,
>
> > > I have two ActiveRecord classes, Player and User. Both underlying
> > > tables have a column called email.
>
> > > class User < ActiveRecord::Base
> > > end
>
> > > class Player < ActiveRecord::Base
> > > belongs_to :user
> > > end
>
> > > If the player instance's user attribute is not nil I want any reference
> > > to player.email get or set player.user.email - so I tried this...
>
> > > class Player < ActiveRecord::Base
> > > belongs_to :user
> > > def email
> > > return user.email if !(user.nil? || user.empty?)
> > > end
> > > end
>
> > > ... but it doesn't work. When I retreive a relevant player object and
> > > try player.email I still get the email address from the player rather
> > > than the user.
>
> > > Any ideas where I am going wrong?
>
> > > MartinMartin,
>
> > Try this:
>
> > def email
> > user ? user.email : self[:email]
> > end
>
> > --
> > Zack Chandlerhttp://depixelate.comhttp://trackplace.com
>
Martin,
p.email => "user-email@dot.com" # what i want
This is good.
p[:email] => "player-email@dot.com" # not what i want
This is not good because it bypasses the accessor that you just wrote.
The reason is ActiveRecord::Base overrides like this:
def (attr_name)
read_attribute(attr_name)
end
... so it reads the actual column data. Use the first approach.
In console I have the choice to use p.email, but when my form displays
it is still showing the data from the player object, bypassing my
overridden method.
I assume that <%= text_field 'player', 'email' %> must effectively
still call read_attribute(attr_name) rather than use the accessor.
The problem is that text_field uses *_before_type_cast accessors to get data.
I see two solutions:
1. overriding *_before_type_cast accessor (as you described)
2. adding virtual field (that has no underlying column). E.g. in your case
this could be done like this:
class Player
def user_or_player_email
return user.email if user
self.email
end
def user_or_player_email=(value)
self.email = value
end
end
The trick here is that text_field will use plain value (not _before_type_cast
variant) if attribute doesn't support _before_type_cast accessor.
Although, I would prefer using second variant.
You could rename players.email column to something like "custom_email"
or "player_email" and use "email" as the name of virtual attribute.
No, dynamically generated accessors for DB columns do not overwrite existing
accessors. Dynamic accessor don't get generated if there is method with the
same name already.