Association error in a namespace using Ruby on Rails 3
In 'ROOT_RAILS/models/users/account.rb' I have
has_one :pro开发者_如何学Cfile,
:primary_key => "app_profile_id",
:foreign_key => "id",
:dependent => :destroy
In 'ROOT_RAILS/models/apps/profile.rb' I have
belongs_to :user,
:primary_key => "id",
:foreign_key => "app_profile_id"
In 'ROOT_RAILS/config/routes.rb' I have
namespace "users" do
resources :accounts
end
namespace "app" do
resources :profiles
end
When I try to access @account.profile (@account is an account ActiveRecord), for example in a '.html.erb' file, I get this error:
uninitialized constant Users::Account::Profile
What/where is the problem?
The following should achieve what you're trying to do:
routes.rb:
resources :users do
resource :profile
end
accounts.rb:
has_one :profile, :primary_key => "app_profile_id",
:dependent => :destroy
profile.rb:
belongs_to :user,
:foreign_key => "app_profile_id"
Make sure the profiles table contains a column for the foreign key too.
After a little bit of headache I found the solution:
has_one :profile,
:class_name => "Apps::Profile",
:primary_key => "app_profile_id",
:foreign_key => "id",
:dependent => :destroy
精彩评论