使用 has_many 的错误:通过关联

发布于 2025-01-05 00:36:36 字数 1483 浏览 0 评论 0原文

我正在尝试创建一个 has_many :through 关联,以便我的用户在将域添加到其帐户页面时可以通过创建关联来跟踪域。

我有以下模型:

models/user.rb

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :domain_followings, :foreign_key => "domain_id"
  has_many :domains, :through => :domain_followings

models/domain_followings.rb

class DomainFollowings < ActiveRecord::Base
  attr_accessible :domain_id

  belongs_to :user
  belongs_to :domain
end

models/domain.rb

class Domain < ActiveRecord::Base
  attr_accessible :name

  has_many :domain_followings, :foreign_key => "user_id"
  has_many :users, :through => :domain_followings

我在规范中遇到如下错误:

   1) Users signup success should make a new user
     Failure/Error: click_button
     NameError:
       uninitialized constant User::DomainFollowing
     # ./app/controllers/users_controller.rb:32:in `show'
     # ./spec/requests/users_spec.rb:32
     # ./spec/requests/users_spec.rb:26

有问题的代码在这里:

  def show
    @user = User.find(params[:id])
    @title = @user.name
    @domain = Domain.new if signed_in?
    @domains = @user.domains.paginate(:page => params[:page])
  end

The uninitializedconstant User::DomainFollowing is重复几次其他代码。 理想情况下,我希望我的代码在允许在域表中创建域之前要求在domain_followings 表中进行用户关联。这可能吗?

I am trying to create a has_many :through association so that my Users can keep track of Domains by creating the association when they add them on their account page.

I have the following models:

models/user.rb

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :domain_followings, :foreign_key => "domain_id"
  has_many :domains, :through => :domain_followings

models/domain_followings.rb

class DomainFollowings < ActiveRecord::Base
  attr_accessible :domain_id

  belongs_to :user
  belongs_to :domain
end

models/domain.rb

class Domain < ActiveRecord::Base
  attr_accessible :name

  has_many :domain_followings, :foreign_key => "user_id"
  has_many :users, :through => :domain_followings

I get errors like the following in my specs:

   1) Users signup success should make a new user
     Failure/Error: click_button
     NameError:
       uninitialized constant User::DomainFollowing
     # ./app/controllers/users_controller.rb:32:in `show'
     # ./spec/requests/users_spec.rb:32
     # ./spec/requests/users_spec.rb:26

The code in question is here:

  def show
    @user = User.find(params[:id])
    @title = @user.name
    @domain = Domain.new if signed_in?
    @domains = @user.domains.paginate(:page => params[:page])
  end

The uninitialized constant User::DomainFollowing is repeated several times for other code.
Ideally I would like my code to require a user association in the domain_followings table before allowing a domain to be created in the domain table. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会发光的星星闪亮亮i 2025-01-12 00:36:36

解决方案是将关系模型重命名为 models/domain_following.rb,使其单一。还根据 Bladrick 的建议删除了foreign_key 声明。现在一切都很顺利。

Solution was to rename the relationship model to models/domain_following.rb, making it singular. Also removed foreign_key declarations following Bladrick's suggestion. Everything flows smoothly now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文