我无法获取我的CheckIn记录以保存,因为关联的租约未保存。Rails/ActiveRecord - 关联不保存
我有三个型号,协会:
class Property < ApplicationRecord
has_many :tenancies
end
class Tenancy < ApplicationRecord
belongs_to :property
has_many :check_ins
end
class CheckIn < ApplicationRecord
belongs_to :tenancy
accepts_nested_attributes_for :tenancy
end
我想签入新的行动创造双方签入和相关租赁:
def new
@check_in = CheckIn.new
@check_in.build_tenancy.property_id = params[:property_id]
end
我,否则以包括PROPERTY_ID部分租赁不会节省。
在check_ins形式/ new.html.erb:
<%= form_for @check_in, url: property_check_ins_path do |f| %>
<%= f.label :date_time %>
<%= f.datetime_select :date_time, {minute_step: 15} %>
<%= f.label :tenancy %>
<%= f.fields_for :tenancy do |i| %>
<%= i.date_select :start_date %>
<% end %>
<%= f.submit "Create Check In" %>
<% end %>
我添加租赁属性与CheckInsController强PARAMS:
def check_in_params
params.require(:check_in).permit(:tenancy_id, :date_time, tenancy_attributes: [:start_date])
end
值得一提的是,check_ins路线是嵌套在属性:
resources :properties do
resources :check_ins, only: [:new, :create]
end
所以问题是,当我得到到CheckInsController中的创建操作,我建立的租约已经消失。我不确定每个记录应该如何以及何时保存,而我试图实现的轻微复杂性使得找到相关帮助非常困难,因此有任何想法?
我正在使用Rails 5.
您是否在strong_params –
中添加了'tenancy_attributes'我添加了原始问题,谢谢 –
啊,我刚刚意识到我没有:允许的params中的tenancy_id ..但后来我把它并且它仍然不能保存 –