我正在尝试创建一个has_many:通过关联,以便我的用户可以通过在他们的帐户页面上添加关联时创建关联来跟踪域。
我有以下型号:
模型/ 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
模型/ domain_followings.rb
class DomainFollowings < ActiveRecord::Base
attr_accessible :domain_id
belongs_to :user
belongs_to :domain
end
模型/ 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
对于其他代码,未初始化的常量User :: DomainFollowing重复多次。 理想情况下,我希望我的代码在domain_followings表中要求用户关联,然后才允许在域表中创建域。这可能吗?
答案 0 :(得分:0)
解决方案是将关系模型重命名为models / domain_following.rb,使其成为单数。在Bladrick的建议之后也删除了foreign_key声明。现在一切都顺利进行。