Ruby on Rails:以一种形式更新多个模型 [英] Ruby on Rails : Updating multiple models in a single form

查看:157
本文介绍了Ruby on Rails:以一种形式更新多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个模型:用户,房屋和订购.

I have 3 models User, House and Order.

订单模型

class Order < ActiveRecord::Base
   belongs_to :from_house, :class_name => "House"
   belongs_to :to_house, :class_name => "House"
   belongs_to :user

   accepts_nested_attributes_for :from_house, :to_house, :user
end

我的房子模型.

class House < ActiveRecord::Base
  belongs_to :user
  belongs_to :place
  belongs_to :city
end

我的用户模型.

class User < ActiveRecord::Base
  has_many :orders
  has_many :houses
end

在我的订单中,我有类似的东西

In my order form I have something like this

<%= form_for @order do |f| %>
  ... # order fields
  <%= f.fields_for :user do |i| %>
    ... # your from user forms
  <% end %>
  <%= f.fields_for :from_house do |i| %>
    ... # your from house forms
  <% end %>
  <%= f.fields_for :to_house do |i| %>
    ... # your to house forms
  <% end %>
  ...
<% end %>

我对控制器的默认设置没有太大的改变.控制器代码

I haven't changed much in controller from the default. The controller code

  def create
    @order = Order.new(order_params)

    respond_to do |format|
      if @order.save
        format.html { redirect_to @order, notice: 'Order was successfully created.' }
        format.json { render action: 'show', status: :created, location: @order }
      else
        format.html { render action: 'new' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end

    def order_params
      params.require(:order).permit( :shift_date, user_attributes: [:name, :email, :ph_no], from_house_attributes: [:place_id, :floor, :elevator, :size], to_house_attributes: [:place_id, :floor, :elevator])
    end

当我提交表单时,正如预期的那样,将使用新的from_house和to_house以及新用户创建订单.但是,我的用户表中的user_id仍然为NULL.我该如何使房屋(从房屋到房屋)都引用提交后创建的用户.

When I submit the form, as expected a Order gets created with a new from_house and to_house along with a new user. But however my user_id in house table remains NULL. How can I make the houses(both from and to) reference the user created after submit.

该用户尚未登录,因此没有current_user.我们必须根据给定的详细信息创建一个新用户.该用户必须与房屋(从和到)相关联.

The User is not logged in, So there is no current_user. We have to create a new user based on the details given. That user has to be associated with the houses (from and to).

我希望我明白.如果没有,请告诉我.

I hope I'm clear. If not please let me know.

PS:此问题是对此

P.S: This question is an extension to this Ruby on rails: Adding 2 references of a single model to another model

推荐答案

我认为app/models/order.rb中的这一更改应该可以解决问题:

I think this change in app/models/order.rb should do the trick:

class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :from_house, class_name: 'House'
  belongs_to :to_house, class_name: 'House'

  accepts_nested_attributes_for :user, :from_house, :to_house

  validates :user, :from_house, :to_house, presence: true

  def from_house_attributes=(attributes)
    fh = build_from_house(attributes)
    fh.user = self.user
  end

  def to_house_attributes=(attributes)
    th = build_to_house(attributes)
    th.user = self.user
  end
end

现在,在Rails控制台中尝试以下操作:

Now, try this in your Rails console:

params = { user_attributes: { name: 'New name', email: 'name@example.com' }, from_house_attributes: { name: 'From house name' }, to_house_attributes: { name: 'to house name' } }
o = Order.new(params)
o.save
o.from_house
o.to_house

干杯!

这篇关于Ruby on Rails:以一种形式更新多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆