我有两个模型:
Clients
validates :first_name, presence: true
validates :last_name, presence: true
#scope :with_current_copay, joins(:insurance_providers).merge(InsuranceProvider.max.group(:client_id))
has_many :appointments
has_many :insurance_providers
accepts_nested_attributes_for :insurance_providers
belongs_to :users
end
和InsuranceProviders
(属于客户)。
class InsuranceProvider < ActiveRecord::Base
#scope :max, -> maximum(:effective_on, :group => 'client_id')
belongs_to :client
end
我创建了一个表单,它与 InsuranceProvider 一起创建了一个客户。
InsuranceProvider 有一个名为 client_id 的列。
填写表格时,client_id 还不存在。如何将该 client_id 放入 InsuranceProvider 表中?
这是我目前的表格:
<%= simple_form_for(@client) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= hidden_field_tag :user_id, current_user.id %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.fields_for :insurance_providers do |provider| %>
<%= provider.input :name, label: 'Insurance provider' %>
<%= provider.input :member_id, label: 'Member ID' %>
<%= provider.input :copay, as: :integer %>
<%= provider.input :effective_on, as: :date %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
数据库迁移:
class CreateInsuranceProviders < ActiveRecord::Migration
def change
create_table :insurance_providers do |t|
t.integer :client_id
t.string :name
t.string :member_id
t.integer :copay
t.date :effective_on
t.timestamps
end
end
end