I am working with two models: Locations and Products
Locations is the parent and products is the child. I previously had these records being created separately, first having the user create the Location and then the Product.
I am now using nested_forms to reduce the extra step.
The issue I am having now is with the formatted URL creation (using friendly_id)
Previously, as you can see below, my url was being created by elements gathered from its parent.
The issue I am having now is since the parent is being created at the same time as the child, it is not able to allocate the Location's before hand and issue it to the child until after it has been saved. This in return is not allowing my product to pick up the Location fields I used before to generate my url.
Is there a way for me to gather all the location information, pass it to the product before the product is created so it can produce the url string I need?
Location Model
class Location < ActiveRecord::Base
has_many :products, dependent: :destroy
accepts_nested_attributes_for :products, :allow_destroy => :true
validates_associated :products
end
Product Model
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
Locations Controller
params.require(:location).permit(:name, :slug, :media_type, :city, :state, :zipcode, etc...,
products_attributes: [:id, :name, :sku, etc...])
Log
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)