Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Modify column names - tried alias_attribute

activerecord alias_attribute alias

  • Please log in to reply
2 replies to this topic

#1 Bardia

Bardia

    Passenger

  • Members
  • 2 posts

Posted 18 September 2013 - 11:58 PM

I have  a model named User, with the following fields: FirstName, LastName

 

In the model I'm using the alias_attribute method to access these fields using underscore naming convetions

 

ie.

 

alias_attribute :first_name, :FirstName

alias_attribute :last_name, :LastName

 

This allows me to the following:

 

User.last.first_name

 

and I get something like "Bob" back.

 

 

Now, when I do something like User.last, I still get the original column names

 

... FirstName: "Bob", LastName: "Smith"...

 

I'd like to receive the alias attributes back instead, something like this

 

... first_name: "Bob", last_name: "Smith"...

 

 

Help?

 

Thanks,

-Bardia



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 19 September 2013 - 07:35 AM

Ugh, FirstName and LastName are not good names for database columns, which I guess is why you want to use first_name and last_name instead.

 

You have several options:

 

Option 1)

When you get the result from User.last, you can run the hash through underscore, to underscore it:

User.last.attributes.inject({}) {|hash, kv| hash[kv[0].underscore] ||= kv[1]; hash}

Option 2)

Write a migration, which changes the FirstName and LastName column to first_name and last_name:

class ChangeNamesOfUserColumns < ActiveRecord::Migration
  def self.up
    add_column :user, :first_name, :string
    add_column :user, :last_name, :string

    User.reset_column_information

    User.find_each do |user|
      user.update_attributes(first_name: user.FirstName, last_name: user.LastName)
    end
    
    remove_column :user, :FirstName
    remove_column :user, :LastName
  end

  def self.down
    # ... do the opposite
  end
end

  • Bardia likes this

Blog: http://ohm.sh | Twitter: madsohm


#3 Bardia

Bardia

    Passenger

  • Members
  • 2 posts

Posted 19 September 2013 - 06:12 PM

 

Ugh, FirstName and LastName are not good names for database columns, which I guess is why you want to use first_name and last_name instead.

 

You have several options:

 

Option 1)

When you get the result from User.last, you can run the hash through underscore, to underscore it:

User.last.attributes.inject({}) {|hash, kv| hash[kv[0].underscore] ||= kv[1]; hash}

Option 2)

Write a migration, which changes the FirstName and LastName column to first_name and last_name:

class ChangeNamesOfUserColumns < ActiveRecord::Migration
  def self.up
    add_column :user, :first_name, :string
    add_column :user, :last_name, :string

    User.reset_column_information

    User.find_each do |user|
      user.update_attributes(first_name: user.FirstName, last_name: user.LastName)
    end
    
    remove_column :user, :FirstName
    remove_column :user, :LastName
  end

  def self.down
    # ... do the opposite
  end
end

This nails what I want to achieve.

 

Moreover, this is a view that's set by data warehousing, therefore not much flexibility as we'd like to apply migrations and change the column names. 

 

Is there a way to inject the underscore into the model?

 

ie.

 

class User < ActiveRecord::Migration

 ....

 

 self.attributes.inject({}) {|hash, kv| hash[kv[0].underscore] ||= kv[1]; hash} #pseudo syntax :)

end







Also tagged with one or more of these keywords: activerecord, alias_attribute, alias

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users