ArgumentError:未知密钥::唯一性。有效键是:: class_name,:anonymous_class,:foreign_key,:validate,:autosave,:table_name,:before_add,:after_add,:before_remove,:after_remove ,:extend,: primary_key ,: dependent,:as,:through,: source,:source_type,:inverse_of,:counter_cache,:join_table,:foreign_type,:index_errors
class League < ActiveRecord::Base
validates :user_id, presence: true
has_one :user
has_many :teams, uniqueness: {
scope: [:user_id],
message: 'You can only create one team per league'
}
end
联盟中有多个团队,但团队必须拥有唯一的user_id。
create_table "leagues", force: :cascade do |t|
t.integer "user_id"
t.string "name"
t.string "key"
t.index ["user_id"], name: "index_leagues_on_user_id"
end
create_table "teams", force: :cascade do |t|
t.integer "user_id"
t.string "name"
t.string "players"
t.integer "league_id"
t.index ["league_id"], name: "index_teams_on_league_id"
t.index ["user_id"], name: "index_teams_on_user_id"
end
validates :user_id, presence: true
和has_one :user
与创建联盟的用户有关。
答案 0 :(得分:0)
ArgumentError:未知密钥::唯一性。有效密钥是:: class_name, :anonymous_class,:foreign_key,:validate,:autosave,:table_name, :before_add,:after_add,:before_remove,:after_remove,:extend, :primary_key,:dependent,:as,:through,:source,:source_type, :inverse_of,:counter_cache,:join_table,:foreign_type,:index_errors
错误说明了一切!您正在将无效密钥(uniqueness
)传递给has_many
关联。我相信您正在尝试进行唯一性验证,而是将其混合在has_many
关联上。我建议你阅读这些Guides