2011-02-01 1 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 '나는이 있습니다

내 RoR에 응용 프로그램에서 구문을

Users::Account.find(1) 

을 계속 사용하고자하는

accounts 
profiles 

,하지만 난 것 :

belongs_to :account 

..는 SQL 데이터베이스에 나는라는 이름의 테이블이 내 SQL 테이블 이름은 다음과 같습니다.

users_accounts 
second_profiles 

어떻게 수행하나요?

피씨 : 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