accepts_nested_attributes_for在控制器方法之前运行

时间:2013-04-10 21:12:51

标签: ruby-on-rails nested-attributes

我遇到了一个令人困惑的问题。我有一个LineItem模型,它接受Trackable和Product的嵌套属性。我重写了默认的trackable_attributes =和product_attributes =方法,以允许我查找或初始化记录。例如:

def trackable_attributes=(attributes)
  _trackable = Project.find_or_initialize_by_id(attributes.delete("id"))
  _trackable.attributes = attributes
  self.trackable = _trackable
end

当我发布到LineItemController #create时,即使其中只有一个日志记录语句,它也会运行所有已发布的参数,就像我正在尝试构建/创建新的行项目一样。我没有任何在#create方法上执行此操作的before_filters。

def create
  logger.info("LineItemsController#create")
end

网络服务器日志:

Started POST "/line_items" for 127.0.0.1 at 2013-04-10 14:02:13 -0700
Processing by LineItemsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MY_SECRET_TOKEN", "line_item"=>{"trackable_type"=>"Project", "tax_rate"=>"0", "price"=>"0.00", "provided_on_formatted"=>"Apr 10", "trackable_attributes"=>{"company_attributes"=>{"id"=>"87", "name"=>"Handmade Design"}, "id"=>"45", "name"=>"MEdia Blitz"}, "product_attributes"=>{"id"=>"75"}, "name"=>"Design", "notes"=>"Yellow", "quantity"=>"0.00", "unit_price"=>"205.0"}, "commit"=>"+"}
Person Load (0.3ms)  SELECT "people".* FROM "people" WHERE "people"."id" = 73 LIMIT 1
(0.1ms)  begin transaction
(0.4ms)  UPDATE "people" SET "last_request_at" = '2013-04-10 21:02:13.861629', "perishable_token" = 'kzsB5dnCOHfykO1XMMi9', "updated_at" = '2013-04-10 21:02:13.863622' WHERE "people"."id" = 73
[paperclip] Saving attachments.
(1.2ms)  commit transaction
Account Load (0.2ms)  SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = 369 LIMIT 1
Person Load (0.3ms)  SELECT "people".* FROM "people" WHERE "people"."account_id" = 369 ORDER BY id LIMIT 1
{"company_attributes"=>{"id"=>"87", "name"=>"Handmade Design"}, "id"=>"45", "name"=>"MEdia Blitz"}
Project Load (0.2ms)  SELECT "projects".* FROM "projects" WHERE "projects"."id" = 45 LIMIT 1
Company Load (0.2ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = 87 LIMIT 1
trackable_attributes=(attributes)
trackable assigned
{"id"=>"75"}
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 75 LIMIT 1
product_attributes=(attributes)
product assigned
LineItemsController#create

请注意,我在#create方法开始之前调用的自定义accepts_nested_attributes_for调度器中散布了许多日志记录语句。

这是正常的吗?当我想在LineItemsController #create方法中以常规方式实例化新的line_item时,似乎会引起问题。

我真的很感激任何帮助,因为我迷失在哪里开始。

0 个答案:

没有答案
相关问题