mysql - Creating an object with multipple associations on ruby on rails -


i'm beginner @ ruby on rails , i'm trying create "order" table 2 foreign ids, "user_id" , "room_id". far i'm able fill in 1 foreign id @ 1 time.

here codes 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 relevant codes users controllers

class userscontroller < applicationcontroller     def current_user         channel_id = session[:channel_id]         @current_user ||= user.find_by(id: channel_id)     end end 

here relevant codes rooms controllers

class roomscontroller < applicationcontroller     def current_room         room_id = session[:room_id]         @current_room ||= room.find_by(id: channel_id)     end end 

here relevant codes orders controllers

class orderscontroller < applicationcontroller     def new         @order = order.new     end      def create         @order = current_user.orders.build(order_params)    #only user_id column filled         #@order = current_room.orders.build(order_params)    #only room_id column filled         if @order.save              flash[:success] = "item has been added"             redirect_to all_orders_path         else              flash[:danger] = "item not added!"             render 'new'         end     end      private      def order_params         params.require(:ameens_menu).permit(:meal)     end end 

here codes order form

<% provide(:title, "order form") %> <div>     <div>      <h1>place order</h1>     <%= form_for(@order) |f| %>     <%= f.label :meal %>     <%= f.text_field :meal %>     <%= f.submit "submit order" %>      <% end %>     </div> </div> 

both channel_id , room_id updated when order submitted, 1 id can recorded database.

when use @order = current_room.orders.build(order_params) table such:

|  meal  | room id | user id | |chicken |    4    |  <nil>  | 

when use @order = current_user.orders.build(order_params) table such:

|  meal  | room id | user id | |chicken |  <nil>  |   60    | 

is there way create object @order such there 2 associations , database table can such:

|  meal  | room id | user id | |chicken |    4    |   60    | 

thanks in advance help!

if understand question correctly, can achieve quite easily. in controller can like:

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

hope helps.

good luck!


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -