Rails 我无法正确使用 belongs_to 和 class 命名方法
Rails I can't use belongs_to and class name methods correctly
首先,大家的好论坛。
我正在尝试在 rails 上使用 Ruby 开发一个应用程序,在这个应用程序中将有用户和房间 类,这些 类 将通过 [=35] 相互通信=] 和 creator_id,但我无法执行必要的操作,你能帮忙吗?
创建用户模型:
$ rails g model User first_name last_name username role player_id:integer password
正在创建房间模型:
$ 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
我正在尝试的代码 运行:
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
错误:
/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)
我通过像这样创建房间模型解决了这个问题:
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
感谢所有帮助过的人。
首先,大家的好论坛。 我正在尝试在 rails 上使用 Ruby 开发一个应用程序,在这个应用程序中将有用户和房间 类,这些 类 将通过 [=35] 相互通信=] 和 creator_id,但我无法执行必要的操作,你能帮忙吗?
创建用户模型:
$ rails g model User first_name last_name username role player_id:integer password
正在创建房间模型:
$ 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
我正在尝试的代码 运行:
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
错误:
/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)
我通过像这样创建房间模型解决了这个问题:
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
感谢所有帮助过的人。