Column aliasing in ActiveRecord?


#1

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?

Hope that makes sense.

Wes


#2

something like this?

def y
x
end

def y=(value)
x = value
end


#3

Wes G. wrote:

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


#4

Josh S. wrote:

Wes G. wrote:

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

belongs_to :account

 def login
  foo
 end

def login=(value)
foo = value
end

alias :foo :login
alias :foo= :login=

end

so… how should the define methods look like?


#5

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.

  • James

On 12/6/06, Guest removed_email_address@domain.invalid wrote:

Use Ruby’s ‘alias’ feature.
understand. But I’m a total noob, so how would you for instance alias a
foo

so… how should the define methods look like?


Posted via http://www.ruby-forum.com/.

  • J *
    ~

#6

Wow, that’s an old original post.

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