Anybody know of a way to alias a column in ActiveRecord such that you
could reference it via a different symbol in your view for view
purposes?
Like my column is named X but I want to display it as Y and maybe I have
an accessor named Y set up on the model so that when I “call” Y from the
view (and controller?) I’m actually manipulating the column X?
Anybody know of a way to alias a column in ActiveRecord such that you
could reference it via a different symbol in your view for view purposes?
Like my column is named X but I want to display it as Y and maybe I have
an accessor named Y set up on the model so that when I “call” Y from the
view (and controller?) I’m actually manipulating the column X?
Use Ruby’s ‘alias’ feature.
alias :y :x
alias :y= :x=
Make sure you alias after the :x methods are defined, because a later
‘def x’ will create a new :x method and :y will point to the old :x.
Anybody know of a way to alias a column in ActiveRecord such that you
could reference it via a different symbol in your view for view purposes?
Like my column is named X but I want to display it as Y and maybe I have
an accessor named Y set up on the model so that when I “call” Y from the
view (and controller?) I’m actually manipulating the column X?
Use Ruby’s ‘alias’ feature.
alias :y :x
alias :y= :x=
Make sure you alias after the :x methods are defined, because a later
‘def x’ will create a new :x method and :y will point to the old :x.
–josh
this is probably well and all for an experianced programmer to
understand. But I’m a total noob, so how would you for instance alias a
column name namned “login” to the alias “foo” in a table namned “users”
Tried something like this… and it does not work:
class User < ActiveRecord::Base
include LoginEngine::AuthenticatedUser
If you’re trying to bend the login engine plugin into a schema that
really doesn’t match its assumptions, you’re probably better copying
the code into your application properly, and then just adjusting it
and it’s migrations directly - this kind of hacking (while certainly
possible) is just going to make things more complicated in the long
run…
FYI, ActiveRecord has an ‘alias_attribute’ method which does exactly
what you’re looking for, i.e.
class MyModel < ActiveRecord::Base
alias_attribute :your_name, :the_actual_column_name
end
This might only be in Edge Rails though; Caveat Coder.
When I need to do display that modifies the original attribute in some
way on the way in or out, I do something like this:
def zip_code
zip = read_attribute(:Zip)
zip.nil? ? ‘’ : zip.strip
end
def zip_code=(new_zip)
write_attribute(:Zip, new_zip.strip)
end
where “Zip” is the column in the table. I don’t use method aliasing to
accomplish this although I suppose that I could. I tend to like
explicitly using the read/write attribute functions to remind me of all
of the layers of attribute management which are involved.
Wes
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.