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.

3
  • See stackoverflow.com/questions/4014831/… Commented Oct 15, 2012 at 3: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. Commented Oct 15, 2012 at 4: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. Commented Oct 15, 2012 at 5:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I have over 50 tables. Is there a way to automatically run over each table in this fashion?
Do you have Model classes for all these 50 tables?
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?
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.
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.
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
....

1 Comment

Hi. Do you know of any possible caveats to this?