Search code examples
rubymodel-associationssequelpadrino

How to add stuff I associated to migrations?


I have a seemingly easy problem, but I have been unable to find a proper explanation (or I've just been too tired to see it). I have following migrations and models:

    class User < Sequel::Model
            one_to_many :orders
    end

    Sequel.migration do
            change do
                    create_table :users do
                            primary_key :id
                            String :name, :null=> false
                            String :password, :null=>false
                            create_table :user_orders do
                                  #like that? If so, what to put in here?
                            end
                    end    
            end
    end

and

    class Order < Sequel::Model
            many_to_one :user
            many_to_one :restaurant #ignore for now
    end

    Sequel.migration do
            change do
                    create_table :orders do
                            primary_key :id
                           #no stuff yet
                    end
            end


    end

My question is - how do I create a table in users and a single entry in orders to assign proper orders to user and proper user to an order. If I skipped some obvious source then I'm sorry in advance (new to ruby and whole web apps thing), and by all means point me in the right direction.


Solution

  • I'm not sure if that is what you want, but as you can see in the documentation, the :orders table should contain a foreign key to :users:

    Sequel.migration do
      change do
        create_table :orders do
          primary_key :id
          foreign_key :user_id, :users
          #no stuff yet
        end
      end
    end