mercredi 24 mai 2017

Trying to make associations with Rails

i'm learning Rails and i'm doing an exercise to practice associations and migration files.

Currently, trying to make a models between users, auction item, and bids.

So far for the migrate files I have the following:

    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

end

These are the following models:

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

Any suggestions or opinions? I'm currently trying to test the tables but auctions doesn't seem to be working... Specifically, my auction table can't seem to find a user_id and therefore a user doesn't have any auctions.

Aucun commentaire:

Enregistrer un commentaire