简体   繁体   中英

Creating an object with multiple associations on ruby on rails

I'm a beginner at ruby on rails and I'm trying to create an "Order" table with 2 foreign IDs, "user_id" and "room_id". So far I'm only able to fill in one foreign ID at any one time.

Here are the codes for the models.

class User < ActiveRecord::Base
    has_many: orders, dependent: :destroy
end 

class Room < ActiveRecord::Base
    has_many: orders, dependent: :destroy
end

class Order < ActiveRecord::Base
    belongs_to :user
    belongs_to :room

    validates :user_id, presence: true
    #validates :room_id, presence: true
    validates :meal, presence: true
end

Here are the relevant codes for the Users Controllers

class UsersController < ApplicationController
    def current_user
        channel_id = session[:channel_id]
        @current_user ||= User.find_by(id: channel_id)
    end
end

Here are the relevant codes for the Rooms Controllers

class RoomsController < ApplicationController
    def current_room
        room_id = session[:room_id]
        @current_room ||= Room.find_by(id: channel_id)
    end
end

Here are the relevant codes for the Orders Controllers

class OrdersController < ApplicationController
    def new
        @order = Order.new
    end

    def create
        @order = current_user.orders.build(order_params)    #only user_id column will be filled
        #@order = current_room.orders.build(order_params)    #only room_id column will be filled
        if @order.save 
            flash[:success] = "Item has been successfully added"
            redirect_to all_orders_path
        else 
            flash[:danger] = "Item was not added!"
            render 'new'
        end
    end

    private 
    def order_params
        params.require(:ameens_menu).permit(:meal)
    end
end

Here are the codes for the order form

<% provide(:title, "Order Form") %>
<div>
    <div>

    <h1>Place an order</h1>
    <%= form_for(@order) do |f| %>
    <%= f.label :Meal %>
    <%= f.text_field :meal %>
    <%= f.submit "Submit my order" %>

    <% end %>
    </div>
</div>

Both the channel_id and the room_id are updated when the order is submitted, but only one id can be recorded into the database.

When I use @order = current_room.orders.build(order_params) the table will be as such:

|  Meal  | Room ID | User ID |
|Chicken |    4    |  <nil>  |

When I use @order = current_user.orders.build(order_params) the table will be as such:

|  Meal  | Room ID | User ID |
|Chicken |  <nil>  |   60    |

Is there any way to create the object @order such that there are 2 associations and the database table can be as such:

|  Meal  | Room ID | User ID |
|Chicken |    4    |   60    |

If I understand your question correctly, you can achieve this quite easily. In your controller you can do something like:

class OrdersController < ApplicationController
    # ...

    def create
        @order = current_user.orders.build(order_params)
        @order.room = current_room   # association to room set here
        if @order.save 
           flash[:success] = "Item has been successfully added"
           redirect_to all_orders_path
        else 
            flash[:danger] = "Item was not added!"
            render 'new'
        end
    end

    # ...
end

Hope that helps.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM