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 に答える