1
我有三個模型,在這裏他們是當我嘗試創建has_many時。我基本上希望我的用戶(使用設計)有很多類別。和類別有很多用戶。用戶has_many協會不工作(錯誤:無法找到關聯:model_type中的user_categories)
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
has_many :user_categories
has_many :categories, through: :user_categories
acts_as_messageable
def mailboxer_email(object)
email
end
end
userCategory.rb
class UserCategory < ActiveRecord::Base
belongs_to :user
belongs_to :category
accepts_nested_attributes_for :categories
end
Category.rb
class Category < ActiveRecord::Base
has_many :user_categories
has_many :user, through: :user_categories
validates :name, presence: true, length: {minimum: 3, maximum: 25}
validates_uniqueness_of :name
end
當我運行category.users < <用戶我得到這個錯誤:
的ActiveRecord :: HasManyThroughAssociationNotFoundError:找不到關聯:user_categories模型類別