0

我有以下表格:用户、默认价格、客户、客户价格。我想要的是在创建新客户端时让 default_prices 自动填写 client_prices 表。看起来我可以使用与此答案类似的解决方案。我想要发生的是,当用户创建新客户端时,客户端的所有 client_prices 都由 default_prices 表填充。

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :stripe_token, :default_prices_attributes
  has_many :default_prices
  has_many :clients
  accepts_nested_attributes_for :default_prices, :allow_destroy => true


class DefaultPrice < ActiveRecord::Base
  attr_accessible :price, :user_id, :visit_type, :id
  belongs_to :user
end


class Client < ActiveRecord::Base
  attr_accessible :active, :address_1, :address_2, :city, :email, :first_name, :last_name, :state, :user_id, :zip, :client_prices_attributes
  belongs_to :user
  has_many :client_prices
  accepts_nested_attributes_for :client_prices_attributes


class ClientPrice < ActiveRecord::Base
  attr_accessible :price, :client_id, :visit_type, :id
  belongs_to :uclient
end
4

1 回答 1

0

您可以添加 before_create 块来复制默认价格。

before_create do
  user.default_prices.each do |default_price|
    client_prices.build(default_price.attributes.slice("price", "visit_type"))
  end
end
于 2012-12-16T06:04:53.487 回答