Ruby on rails 如何将另一个模型的数据写入该模型?

Ruby on rails 如何将另一个模型的数据写入该模型?,ruby-on-rails,ruby,model-view-controller,ruby-on-rails-5,ruby-on-rails-6,Ruby On Rails,Ruby,Model View Controller,Ruby On Rails 5,Ruby On Rails 6,我有一个模型订单和用户 user - has_many: order order - belongs_to: user 我想在订单上核对一下 <% if current_user%>     form where the name of the order form will be recorded from the user  <% else%>     regular order creation form  <% end%> 任务是如何将另

我有一个模型订单和用户

user - has_many: order

order - belongs_to: user
我想在订单上核对一下

<% if current_user%>

    form where the name of the order form will be recorded from the user

 <% else%>

    regular order creation form

 <% end%>

任务是如何将另一个模型的数据写入模型?

您可以在控制器中执行此操作

例如

Class OrdersController < ApplicationController
  def new
    if current_user
      # this will set the user_id field 
      # you can set additional attributes here
      @order = current_user.orders.build({attribute: value})
    else
      @order = Order.new
    end
  end
  ...
你可以试试

= form_for @order do |f|
  ...