Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Ruby on Rails 3的命名空间中的关联错误_Ruby On Rails_Routing_Namespaces_Ruby On Rails 3_Associations - Fatal编程技术网

Ruby on rails 使用Ruby on Rails 3的命名空间中的关联错误

Ruby on rails 使用Ruby on Rails 3的命名空间中的关联错误,ruby-on-rails,routing,namespaces,ruby-on-rails-3,associations,Ruby On Rails,Routing,Namespaces,Ruby On Rails 3,Associations,在“ROOT_RAILS/models/users/account.rb”中,我有 has_one :profile, :primary_key => "app_profile_id", :foreign_key => "id", :dependent => :destroy belongs_to :user, :primary_key => "id", :foreign_key => "app_profile_id" namespace "

在“ROOT_RAILS/models/users/account.rb”中,我有

has_one :profile,
  :primary_key => "app_profile_id",
  :foreign_key => "id",
  :dependent => :destroy
belongs_to :user,
  :primary_key => "id",
  :foreign_key => "app_profile_id"
namespace "users" do
  resources :accounts
end

namespace "app" do
  resources :profiles
end
在“ROOT_RAILS/models/apps/profile.rb”中,我有

has_one :profile,
  :primary_key => "app_profile_id",
  :foreign_key => "id",
  :dependent => :destroy
belongs_to :user,
  :primary_key => "id",
  :foreign_key => "app_profile_id"
namespace "users" do
  resources :accounts
end

namespace "app" do
  resources :profiles
end
在“ROOT_RAILS/config/routes.rb”中,我有

has_one :profile,
  :primary_key => "app_profile_id",
  :foreign_key => "id",
  :dependent => :destroy
belongs_to :user,
  :primary_key => "id",
  :foreign_key => "app_profile_id"
namespace "users" do
  resources :accounts
end

namespace "app" do
  resources :profiles
end
当我尝试访问@account.profile(@account是一个account ActiveRecord)时,例如在“.html.erb”文件中,我遇到以下错误:

uninitialized constant Users::Account::Profile

问题出在哪里?

以下几点应该可以实现您的目标:

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"

确保profiles表中也包含一列外键。

在有点头痛之后,我找到了解决方案:

has_one :profile,
:class_name => "Apps::Profile",
:primary_key => "app_profile_id", 
:foreign_key => "id",
:dependent => :destroy
我尝试了“:foreign\u key=>”id“”而不是“:foreign\u key=>”app\u profile\u id”,以便确保profiles表中有外键。不管怎样,它不起作用。。。可能是因为“account.rb”和“profile.rb”分别位于“/models/users”和“models/apps”子文件夹中。这可能是问题所在吗?