Alexis_user
Alexis_user

Reputation: 425

Creating model with rails

Now, I'm unable to change my db. (many millions rows in each table)

But I would like to create my rails app though.

My DB is absolutely not standard.

Table name : something_people Fields : stid, stname, stfirstname, stage

I know to change the table_name:

class People < ActiveRecord::Base
  self.table_name = "something_people"
end

I would like to rename too the fields. To use the symbol in my rails app and when I'll change the db structure, I only need to change the model classes.

class People < ActiveRecord::Base
  self.table_name = "something_people"
  self.field_name(:id) = "stid"
  self.field_name(:name) = "stname"
  self.field_name(:firstname) = "stfirstname"
  self.field_name(:age) = "stage"
end

an example of query :

@countid = People.where(hash_of_conds).count(:id)

is

SELECT COUNT(stid) FROM something_people WHERE myconditions;

Asked question : How to do what I want to do ?

If you don't understand, tell me.

Thank you.

Upvotes: 0

Views: 29

Answers (1)

usha
usha

Reputation: 29349

you can create aliases

class User < Activerecord::Base
  alias_attribute :id, :stid
  alias_attribute :name, :stname
end

But when you have a query like this

User.where("stname like '%ab%'")

you will have to specify the actual column name in the database

Upvotes: 1

Related Questions