使用 has_many 的错误:通过关联
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是将关系模型重命名为 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.