Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

Rails 5: ArgumentError: Unknown key: :uniqueness

ArgumentError: Unknown key: :uniqueness. Valid keys are: :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

There are multiple teams in a league, but the teams must have a unique 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 and has_one :user is in relation to the User who created the league.

Upvotes: 1

Views: 3028

Answers (1)

Pavan
Pavan

Reputation: 33542

ArgumentError: Unknown key: :uniqueness. Valid keys are: :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

The error says it all! You are passing an Invalid key(uniqueness) to the has_many association. I believe you are trying to do a uniqueness validation and instead mixing it up on a has_many association. I suggest you to read these Guides

Upvotes: 0

Related Questions