How to set associations using namespaces and SQL tables with different names from those of Ruby on Rails models?
I am using Ruby on Rails 3 and a SQL database. I'm trying to rename database tables in order to reach "harmony of names" between RoR Models and SQL tables.
For example...
... in 'RAILS_ROOT/config/routes.rb' I have this:
namespace "users" do
resources :accounts
end
namespace "second" do
resources :profiles
end
... in 'RAILS_ROOT/models/account.rb' I have this:
has_one :profile,
:class_name => "Second::Profile"
... in 'RAILS_ROOT/models/profile.rb' I have this:
belongs_to :accou开发者_StackOverflow中文版nt
.. in the SQL database I have tables named:
accounts
profiles
I would like to continue to use the syntax
Users::Account.find(1)
in my RoR application, but I would like to have my SQL tables name like so:
users_accounts
second_profiles
How to do that?
P.S.: I read ActiveRecord::Base "table_name()" and "table_name_prefix" sections but I am not able to set those.
Use the following code in your models:
class User
set_table_name "users_accounts"
end
class Profile
set_table_name "second_profiles"
end
I hope it helps.
精彩评论