2011-02-01 55 views
0

我正在使用Ruby on Rails 3和一個SQL數據庫。我試圖重命名數據庫表,以達到RoR模型和SQL表之間的「名稱和諧」。如何使用具有Ruby on Rails模型名稱的名稱空間和SQL表來設置關聯?


例如...

...在 'RAILS_ROOT /配置/ routes.rb中' 我有這樣的:

namespace "users" do 
    resources :accounts 
end 

namespace "second" do 
    resources :profiles 
end 

...在「RAILS_ROOT /模型/帳戶.RB」我有這樣的:

has_one :profile, 
    :class_name => "Second::Profile" 

...在 'RAILS_ROOT /模型/ profile.rb' 我有這樣的:

belongs_to :account 

..在SQL數據庫我有一個名爲表:

accounts 
profiles 

我想繼續使用語法

Users::Account.find(1) 

在我的RoR應用程序,但我會喜歡讓我的SQL表名如下:

users_accounts 
second_profiles 

如何做到這一點?

P.S .:我讀ActiveRecord::Base「table_name()」和「table_name_prefix」部分,但我無法設置這些部分。

回答

2

使用下面的代碼在你的模型:

class User 
    set_table_name "users_accounts" 
end 

class Profile 
    set_table_name "second_profiles" 
end 

我希望它能幫助。

+0

在我的情況下,我必須從這裏設置'self.table_name_prefix ='users_'':http://apidock.com/rails/ActiveRecord/Base/table_name/class – user502052 2011-02-01 15:45:07