在创建子对象之前捕获父信息

时间:2014-05-16 14:35:08

标签: ruby-on-rails-4 nested-forms friendly-id before-save

我正在使用两种模式:地点和产品

地点是父级,产品是孩子。我以前曾单独创建这些记录,首先让用户创建位置,然后创建产品。

我现在正在使用nested_forms来减少额外的步骤。

我现在遇到的问题是使用格式化的URL创建(使用friendly_id)

以前,正如您在下面看到的那样,我的网址是由从其父级收集的元素创建的。

我现在遇到的问题是,由于父母与孩子同时被创建,因此无法事先分配位置并将其发送给孩子,直到孩子被保存为止。 。作为回报,这不允许我的产品拿起我以前用过的位置字段来生成我的网址。

我是否有办法收集所有位置信息,在产品创建之前将其传递给产品,以便生成我需要的网址字符串?

位置模型

class Location < ActiveRecord::Base

 has_many :products, dependent: :destroy
 accepts_nested_attributes_for :products, :allow_destroy => :true
 validates_associated :products

end

产品型号

class Product < ActiveRecord::Base

 extend FriendlyId
 friendly_id :slug_candidates, use: :slugged

 def slug_candidates
  [
    [location.media_type, location.state, location.name, :name, :sku]
  ]
 end

位置控制器

  params.require(:location).permit(:name, :slug, :media_type, :city, :state, :zipcode, etc...,
     products_attributes: [:id, :name, :sku, etc...])

日志

Started POST "/locations" for 127.0.0.1 at 2014-05-16 08:51:01 -0700
Processing by LocationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vD4e68ZoJUD0Qp/XUS1Co4nzqyZ6Zf6fshdFL2/7TXA=",  "location"=>{"name"=>"Testing", "vendor_id"=>"1", "media_type"=>"Digital Billboards", "market"=>"SF   Bay Area", "city"=>"santa clara", "state"=>"CA", "zipcode"=>"95124", "longitude"=>"", "latitude"=>"", "installation_side"=>"", "ad_rotation_frequency"=>"", "ad_spot_duration"=>"", "weekly_cycle"=>"", "board_width"=>"", "board_height"=>"", "ad_size_width"=>"", "ad_size_height"=>"", "supported_ad_types"=>"", "age_demographic"=>"", "ethnicity_demo_white"=>"", "ethnicity_demo_black"=>"", "ethnicity_demo_asian"=>"", "ethnicity_demo_hispanic"=>"", "products_attributes"=>{"0"=>{"name"=>"North Face", "sku"=>"1001", "tab_id"=>"", "available_spots"=>"", "dec"=>"", "ooh"=>"", "retail_weekly_price"=>"", "reserve_price"=>"", "description"=>"", "_destroy"=>"false"}}}, "commit"=>"Create Location"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  (0.1ms)  BEGIN
  Location Exists (0.3ms)  SELECT 1 AS one FROM `locations` WHERE `locations`.`slug` = 'testing' LIMIT 1
  Product Exists (0.3ms)  SELECT 1 AS one FROM `products` WHERE `products`.`slug` = 'north-face-1001' LIMIT 1
  Product Exists (0.3ms)  SELECT 1 AS one FROM `products` WHERE `products`.`slug` = 'north-face-1001' AND (`products`.`id` IS NOT NULL) LIMIT 1
  SQL (0.3ms)  INSERT INTO `locations` (`ad_rotation_frequency`, `age_demographic`, `city`, `created_at`, `installation_side`, `market`, `media_type`, `name`, `slug`, `state`, `supported_ad_types`, `updated_at`, `vendor_id`, `weekly_cycle`, `zipcode`) VALUES ('', '', 'santa clara', '2014-05-16 15:51:01', '', 'SF Bay Area', 'Digital Billboards', 'Testing', 'testing', 'CA', '', '2014-05-16 15:51:01', 1, '', 95124)
  Location Load (0.3ms)  SELECT `locations`.* FROM `locations` WHERE `locations`.`id` = 119 ORDER BY `locations`.`id` ASC LIMIT 1
  Vendor Load (0.2ms)  SELECT `vendors`.* FROM `vendors` WHERE `vendors`.`id` = 1 ORDER BY `vendors`.`id` ASC LIMIT 1
  SQL (30.0ms)  INSERT INTO `products` (`created_at`, `description`, `location_id`, `location_name`, `market`, `name`, `sku`, `slug`, `tab_id`, `updated_at`, `vendor_id`, `vendor_name`) VALUES ('2014-05-16 15:51:01', '', 119, 'Testing', 'SF Bay Area', 'North Face', '1001', 'north-face-1001', '', '2014-05-16 15:51:01', 1, 'AdSemble')
   (11.2ms)  COMMIT
   Redirected to http://localhost:3000/locations/testing
   Completed 302 Found in 63ms (ActiveRecord: 43.4ms)

0 个答案:

没有答案