Ruby on Rails : Updating multiple models in a sing

2019-08-29 21:33发布

I have 3 models User, House and Order.

Order Model

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

My House Model.

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

My user model.

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

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.

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.

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

1条回答
迷人小祖宗
2楼-- · 2019-08-29 22:19

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

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

Cheers!

查看更多
登录 后发表回答