Ruby 尝试与Rails建立关联
我正在学习Rails,我正在做一个练习来练习关联和迁移文件 目前,正在尝试在用户、拍卖项目和出价之间建立一个模型 到目前为止,对于迁移文件,我有以下内容:Ruby 尝试与Rails建立关联,ruby,ruby-on-rails-3,associations,Ruby,Ruby On Rails 3,Associations,我正在学习Rails,我正在做一个练习来练习关联和迁移文件 目前,正在尝试在用户、拍卖项目和出价之间建立一个模型 到目前为止,对于迁移文件,我有以下内容: class CreateItem < ActiveRecord::Migration def change create_table :auction do |t| t.string :item_name t.string :condition
class CreateItem < ActiveRecord::Migration
def change
create_table :auction do |t|
t.string :item_name
t.string :condition
t.date :start_date
t.date :end_date
t.text :description
t.timestamps
end
end
end
class CreateBids < ActiveRecord::Migration
def change
create_table :bids do |t|
t.integer :user_id
t.integer :auction_id
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :username
t.string :password_digest
t.timestamps
end
end
class CreateItem
结束
这些是以下型号:
class Bid < ActiveRecord::Base
belongs_to :bidder, class_name: "User", foreign_key: "bidder_id"
belongs_to :auction
end
class User < ActiveRecord::Base
has_many :bids
has_many :auctions, :foreign_key => 'bidder_id'
has_secure_password
end
class Auction < ActiveRecord::Base
belongs_to :seller, class_name: "User", foreign_key: :user_id
has_many :bids
has_many :bidders, through: :bids
end
class-Bid“投标人id”
有安全的密码吗
结束
类拍卖
有什么建议或意见吗?我目前正在尝试测试表,但拍卖似乎不起作用。。。
具体来说,我的拍卖表似乎找不到用户id,因此用户没有任何拍卖。
外键
指的是\u id(默认情况下)或用于关联模型的任何唯一属性
我看不到投标人
模型,您需要将其替换为用户id
,因为它们与用户
模型关联
有关更多详细信息,请参阅拍卖
有哪些?投标人id在哪里?这不应该是user\u id
?是的,我刚刚删除了bidder\u id
,并将其替换为“user\u id”,它现在似乎正在工作,只希望剩下的没问题。把答案贴出来。如果有其他问题出现,你可以搜索/发布另一个问题。我在拍卖中没有看到用户id
,所以卖家将失败。
class CreateBids < ActiveRecord::Migration
def change
create_table :bids do |t|
t.integer :user_id **do not think this is correct**
t.integer :auction_id **or this one**
t.timestamps
end
end
end
class CreateGames < ActiveRecord::Migration[5.0]
def change
create_table :games do |t|
t.integer :total_time
t.references :version, foreign_key: true **#this is how a foreign key should be declared**
t.integer :total_points
t.timestamps
end
end
end
def change
add_reference :levels, :version, foreign_key: true
end