1

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)
RubyNewbie
  • 547
  • 5
  • 21
  • how are you passing the params? are you permitting them? – Babar May 16 '14 at 14:47
  • Yes. They are passed in my locations controller: params.require(:location).permit(...., products_attributes: [:id, :name,...]) – RubyNewbie May 16 '14 at 15:44
  • Can you share the log? – Babar May 16 '14 at 15:47
  • the view and in your products_attributes, why are you passing the id? – Babar May 16 '14 at 15:50
  • @Babar: Sorry, can you rephrase your question? Product_attributes [:id] is being used for nested_forms so it only creates a single record – RubyNewbie May 16 '14 at 15:58
  • sorry, but I don't understand what you are trying to do, you are creating a location and create its products in one form? right? And you want to save all the products in one single record? is this :id in products the same being used by Database(auto increment and unique?) – Babar May 16 '14 at 16:09
  • @Babar Correct, the location and its products are generated in a single form at the same time. I am using the nested_form gem (https://github.com/ryanb/nested_form). The [:id] is used per the docs for this gem which state: "The :id is to make sure you do not end up with a whole lot of tasks (or products in my case)". Everything is being created within the Locations form. Hope that helps – RubyNewbie May 16 '14 at 16:32
  • I do not understand your product is being saved with the location_id, Is your question about association not created? – Babar May 16 '14 at 17:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/53833/discussion-between-babar-and-rubynewbie) – Babar May 16 '14 at 17:19
  • @RubyNewbie I'm having the same problem - have you found a solution? – Matt Dec 09 '14 at 15:43

0 Answers0