0

Is it possible to specify the column for an attribute? I have something like:

NAME, COUNTRY

The database is quite large and I have over a thousand columns which are capitalized like this. I want to refer to them as so:

attr_accessible :name, :country

Where :name = column NAME. I'd prefer Model.name rather than Model.NAME. It isn't possible to downcase every column name in the structure file.

Damien Roche
  • 13,189
  • 18
  • 68
  • 96
  • See http://stackoverflow.com/questions/4014831/alias-for-column-names-in-rails – Alistair A. Israel Oct 15 '12 at 03:59
  • Would you prefer to write a migration to rename all the CAPITALIZED column names to smallcase column names? Then you dont really need to think about the model configurations. And in migration also you can do this in very small code by using reflection. If so, i can help on that. – Samiron Oct 15 '12 at 04:44
  • @Samiron yes, I'd very much prefer to do that. I haven't done that already because it was too cumbersome to do manually. – Damien Roche Oct 15 '12 at 05:08

2 Answers2

1

Here is an idea to do the way you preferred.

Command to generate migration:
(In my example, im applying this on Posts table. Change according to your table name)

rails g migrate RenameColumnsOfPosts

Below is the migration up method. Here taking all the column names and for each one I'm applying rename_column to make it downcase.

class RenameColumnsOfPosts < ActiveRecord::Migration
  def up
     Post.columns.map(&:name).each do |column_name|
        rename_column(:posts, column_name, column_name.downcase)
     end
  end

  def down
     #You can do the opposite operation here. Leaving on you
  end
end

As i didnt run it personally, it might need some changes. So start with it and let me know if facing any problem.

Samiron
  • 5,169
  • 2
  • 28
  • 55
  • I have over 50 tables. Is there a way to automatically run over each table in this fashion? – Damien Roche Oct 15 '12 at 05:57
  • Do you have Model classes for all these 50 tables? – Samiron Oct 15 '12 at 06:02
  • No. They are being imported from a sql dump. Do you think it might be better to just downcase the entire sql structure file before import? – Damien Roche Oct 15 '12 at 06:06
  • If you are planning to dump this sql several times, then I think that would be better option. Anyway, i got a way to run this for all tables. i will update my answer soon. – Samiron Oct 15 '12 at 06:29
  • Oh i missed one thing.. the way i was referring to, requires the **Model class**. Like you can see the answer I already provided using `Post` model. So if you dont have model classes for those tables, it wouldn't be possible. However, it might be possible to access column names directly by running query. That would be a messed up code im sure. So updating in the sql file seems better to me. – Samiron Oct 15 '12 at 06:34
0
Please write code inside of  model ,
it's just for demostration code,after you get it and update as per logic :

This Code inside of model :
...
attr_accessor :name,:country
before_save :fill_save
..
#assign variable like ...
 def fill_save
       self.NAME=  self.name
       self.COUNTRY=  self.country
  end
....
Vikram Jain
  • 5,498
  • 1
  • 19
  • 31