user502052
user502052

Reputation: 15258

Association error in a namespace using Ruby on Rails 3

In 'ROOT_RAILS/models/users/account.rb' I have

has_one :profile,
  :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?

Upvotes: 0

Views: 881

Answers (2)

user502052
user502052

Reputation: 15258

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

Upvotes: 0

clemensp
clemensp

Reputation: 2510

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.

Upvotes: 2

Related Questions