Alexis CHEVREUX
Alexis CHEVREUX

Reputation: 141

Rails Troubles with my relations

I want to have a model Game with have two users: user1 and user2

This is my two models with their migrations:

game.rb:

class Game < ApplicationRecord

has_secure_token

# @!group RELATIONS
has_many :turns, dependent: :destroy
has_many :play_cards, dependent: :destroy
has_one :user1, class_name: 'User', dependent: :destroy
has_one :user2, class_name: 'User', dependent: :destroy
# @!endgroup

# @!group VALIDATORS
validates :user1_id, presence: true, blank: false, nill: false
validates :user2_id, presence: true, blank: false, nill: false
validates :finish_at, presence: true, blank: false, nill: false
validates :token, presence: true, blank: false, nill: false, length: { is: 24 }
# @!endgroup

end

Migration:

def change
create_table :games do |t|
  t.references :user1, foreign_key: { to_table: :users }
  t.references :user2, foreign_key: { to_table: :users }
  t.string :token, length: { is: 24 }, null: false
  t.datetime :finish_at, null: false

  t.timestamps
end
end

user.rb:

class User < ApplicationRecord

has_secure_token :token

# @!group RELATIONS
has_many :decks, dependent: :destroy
has_one :chosen_deck, class_name: 'Deck', dependent: :destroy
has_many :turns, dependent: :destroy
# @!endgroup

# @!group VALIDATORS
validates :hp, presence: true, blank: false, nill: false, numericality: { greater_than_or_equal_to: 0 }
validates :armor, presence: true, blank: false, nill: false, numericality: { greater_than_or_equal_to: 0 }
validates :token, presence: true, blank: false, nill: false, length: { is: 24 }, on: :save
validates :name, presence: true, blank: false, nill: false, length: { minimum: 4 }
# @!endgroup

end

Migration:

create_table :users do |t|
  t.references :chosen_deck, index: true, foreign_key: { to_table: :decks }
  t.string :name, null: false
  t.integer :hp, null: false, default: 20
  t.integer :armor, null: false, default: 0
  t.string :token, length: { is: 24 }, null: false

  t.timestamps
end

The trouble append when I try to save game with that:

ActiveModel::MissingAttributeError: can't write unknown attribute `game_id`

I put a screenshot here: error screen

Have a nice day,

Upvotes: 0

Views: 38

Answers (1)

Igor Drozdov
Igor Drozdov

Reputation: 15045

ActiveModel::MissingAttributeError: can't write unknown attribute game_id

The problem is in exactly what the error says. User model lacks game_id attribute, which is required by convention for connecting a user and a game (if you want another column for that, it should be specified explicitly as a foreign_key)

Just create a game_id column for users and specify (not necessary) belongs_to :game in User model, since user belongs_to a game.

Otherwise, since you have user1_id and user2_id, you need to change has_one to belongs_to. Then it will update these belongs_to columns instead of looking for game_id in the has_one associations.

class Game < ApplicationRecord
  # @!group RELATIONS
  has_many :turns, dependent: :destroy
  has_many :play_cards, dependent: :destroy
  belongs_to :user1, class_name: 'User', dependent: :destroy
  belongs_to :user2, class_name: 'User', dependent: :destroy
  # @!endgroup
end

Upvotes: 4

Related Questions