1

我有一个莫名其妙的问题。我有一个 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 时,即使其中只有一个日志记录语句,它也会运行所有发布的参数,就好像我正在尝试构建/创建一个新的行项目一样。我没有任何 before_filters 可以在 #create 方法上执行此操作。

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

请注意,我有许多日志语句分散在我的自定义 accept_nested_attributes_for 设置器中,这些设置器甚至在 #create 方法开始之前就被调用了。

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

我真的很感激任何帮助,因为我不知道从哪里开始。

4

0 回答 0