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

Fix "unknown column" exception #943

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/friendly_id/sequentially_slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ def self.setup(model_class)
def resolve_friendly_id_conflict(candidate_slugs)
candidate = candidate_slugs.first
return if candidate.nil?
SequentialSlugCalculator.new(scope_for_slug_generator,
candidate,
friendly_id_config.slug_column,
friendly_id_config.sequence_separator,
slug_base_class).next_slug

SequentialSlugCalculator.new(
scope_for_slug_generator,
candidate,
slug_column,
friendly_id_config.sequence_separator,
slug_base_class
).next_slug
end

class SequentialSlugCalculator
Expand Down Expand Up @@ -83,5 +86,13 @@ def slug_base_class
self.class.base_class
end
end

def slug_column
if friendly_id_config.uses?(:history)
:slug
else
friendly_id_config.slug_column
end
end
end
end
15 changes: 15 additions & 0 deletions test/sequentially_slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ def model_class
assert_equal 'test-name-3', record3.slug
end
end

test "should cope with strange column names" do
Journalist = Class.new(ActiveRecord::Base) do
extend FriendlyId
friendly_id :name, :use => [:sequentially_slugged, :history], :slug_column => "strange name"
end

transaction do
record_1 = Journalist.create! name: "Julian Assange"
record_2 = Journalist.create! name: "Julian Assange"

assert_equal 'julian-assange', record_1.attributes["strange name"]
assert_equal 'julian-assange-2', record_2.attributes["strange name"]
parndt marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

class City < ActiveRecord::Base
Expand Down