Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child association slug generation not triggered correctly #515

Closed
glebm opened this issue Jan 13, 2014 · 1 comment
Closed

Child association slug generation not triggered correctly #515

glebm opened this issue Jan 13, 2014 · 1 comment

Comments

@glebm
Copy link

glebm commented Jan 13, 2014

I have a case where a child association is created using standard rails accepts_nested_attributes. I tried making a test case, but couldn't get it running (don't understand where the table structure comes from).

class Company < ActiveRecord::Base
  has_many :job_ads

  extend FriendlyId
  friendly_id :slug_candidates, use: [:slugged, :history]

  def slug_candidates
    [:name, [:name, :summary]]
  end

  def should_generate_new_friendly_id?
    slug.blank? || name_changed?
  end
end

class JobAd < ActiveRecord::Base
  belongs_to :company
  accepts_nested_attributes_for :company

  extend FriendlyId
  friendly_id :slug_candidates, use: [:slugged, :history]

  def slug_candidates
    [:title, [:title, :company_name]]
  end

  def should_generate_new_friendly_id?
    slug.blank? || title_changed?
  end
end

class ChildAssociationTest < MiniTest::Unit::TestCase
  include FriendlyId::Test

  def test_child_association_slug
    JobAd.create!(
        title:              'Job A',
        company_attributes: {name: 'Apple Inc'}
    )
    JobAd.create!(
        title:              'Job B',
        company_attributes: {name: 'Apple Inc'}
    )
  end
end

The second create triggers a PG::UniqueViolation:

ERROR:  duplicate key value violates unique constraint "index_companies_on_slug"
DETAIL:  Key (slug)=(apple-inc) already exists.

How can I have it use a serial or a GUID for the second Apple Inc instead of blowing up? Thanks

Rails 4.0.1 and 4.0.2
friendly_id 5.0.1 and 5.0.2

@PikachuEXE
Copy link
Contributor

Can you try not to use :history first to see if it works?
If it still failed then try #513

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants