0
我想创建一个has_many:通过关联,以便我的用户可以通过创建关联时将它们添加到其帐户页面上来跟踪域。错误使用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
我得到像th这样的错误E在我的规格如下:
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
未初始化常量用户:: DomainFollowing重复几次,其他的代码。 理想情况下,我希望我的代码在domain_followings表中要求用户关联,然后才允许在域表中创建域。这可能吗?
我不认为它是相关的,但我对用户和域中的说明'has_many:domain_followings'的':foreign_key'声明感到困惑。在我看来,他们倒过来(如果不是故意的,它应该被删除)。 – Baldrick 2012-02-12 20:25:23
这是导致保存关系的问题。我也有问题,因为关系的模型是复数而不是单数。我修正了这些问题,并且它运行得更好,但仍然存在很多功能问题。 – Arosboro 2012-02-12 22:06:04