Home > Blockchain >  Rails I can't use belongs_to and class name methods correctly
Rails I can't use belongs_to and class name methods correctly

Time:02-17

First of all, good forum to all. I am trying to develop an application with Ruby on rails and in this application there will be User and Room classes and these classes will communicate with each other with winner_id and creator_id, but I could not do the necessary actions, can you help?

Creating a user model:

$ rails g model User first_name last_name username role player_id:integer password

Creating a room model:

$ rails g model Room rules:text online:boolean room_id:integer password:string winner:belongs_to creator:belongs_to 

User.rb:

class User < ApplicationRecord
    has_many :oda,class_name:"Room",foreign_key:"owner_id"
    has_many :winner,class_name:"Room",foreign_key:"winner_id"
end

Room.rb:

class Room < ApplicationRecord
  belongs_to :owner,class_name:"User",foreign_key:"owner_id"
  belongs_to :winner,class_name:"User",foreign_key:"winner_id"
end

The code I'm trying to run:

User.all

[#<User:0x00005598a714d120
  id: 1,
  first_name: "Lorem",
  last_name: "İpsum",
  username: "Wormer",
  role: "janitor",
  player_id: 1234,
  password: "[FILTERED]",
  created_at: Thu, 10 Feb 2022 15:54:15.312134000 UTC  00:00,
  updated_at: Thu, 10 Feb 2022 15:54:15.312134000 UTC  00:00>,
 #<User:0x00005598a71b8060
  id: 2,
  first_name: "Hello",
  last_name: "World",
  username: "MoonKnight",
  role: "player",
  player_id: 1234,
  password: "[FILTERED]",
  created_at: Thu, 10 Feb 2022 15:55:09.787504000 UTC  00:00,
  updated_at: Thu, 10 Feb 2022 15:55:09.787504000 UTC  00:00>] 

room = Room.new do |room|
  room.rules = "None"
  room.online = true
  room.password = "a"
  room.room_id = 5555
end

room.winner = User.find_by(role:"player")
room.creator = User.find_by(role:"janitor")
room.save

Error:

/home/user/.rvm/gems/ruby-3.0.1/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize': SQLite3::SQLException: no such table: main.owners (ActiveRecord::StatementInvalid)
/home/user/.rvm/gems/ruby-3.0.1/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize': no such table: main.owners (SQLite3::SQLException)

CodePudding user response:

I solved the problem by creating the Room model like this:

class CreateRooms < ActiveRecord::Migration[6.1]
  def change
    create_table :rooms do |t|
      t.text :rules
      t.boolean :online
      t.integer :room_id
      t.string :password
      t.belongs_to :winner, null: false# remove,foreign_key:true
      t.belongs_to :creator, null: false# remove,foreign_key:true

      t.timestamps
    end
    add_foreign_key :rooms, :users, column: :winner_id
    add_foreign_key :rooms, :users, column: :creator_id
  end
end

User.rb:

class User < ApplicationRecord
    has_many :winner_rooms,class_name:"Room",foreign_key:"winner_id"
    has_many :creator_rooms,class_name:"Room",foreign_key:"creator_id"

end

Room.rb:

class Room < ApplicationRecord
  belongs_to :winner,class_name:"User",foreign_key:"winner_id"
  belongs_to :creator,class_name:"User",foreign_key:"creator_id"

end

Thanks to everyone who helped.

  • Related