私はRailsを学んでおり、関連と移行ファイルを練習する練習をしています。Railsとの関連付けを試みようとしています
現在、ユーザー、オークションアイテム、および単価の間でモデルを作成しようとしています。
これまでのところ、以下の私が持っている移行ファイル用:
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 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
任意の提案や意見は?私は現在、テーブルをテストしようとしていますが、オークションは動作していないようです... 特に、私のオークションテーブルはuser_idを見つけることができないため、ユーザーにはオークションがありません。
「オークション」には何がありますか? 'bidder_id'はどこですか?それは 'user_id'ですか? –
ええ、私は 'bidder_id'を削除し、それを 'user_id'に置き換えました。それは今働いているようですが、残りがうまくいくと思っています。 – user7496931
答えをまとめて投稿しました。何か他のものが壊れた場合、別の質問を検索/投稿することができます。 –