Checking for a Discourse topic title
I have a ruby function that creates a discourse topic if the title is not found.
def get_topic(user,title,created_at,bumped_at,last_posted_at,excerpt)
title = title.gsub(/ /,' ')
puts "get_topic.title:'#{title}' user.id:#{user.id} created_at:#{created_at} last_posted_at:#{last_posted_at} excerpt:#{excerpt}"
result = Topic.find_by_title(title)
if result == nil then
result = Topic.create
result.title = title
result.fancy_title = title
result.user_id = user.id
result.last_post_user_id = user.id
result.updated_at = last_posted_at
result.created_at = created_at
result.bumped_at = bumped_at
result.last_posted_at = last_posted_at
result.excerpt = excerpt
result.save!
else
puts result
puts created_at,bumped_at,last_posted_at
if result.user_id != user.id then
result.user_id = user.id
result.last_post_user_id = user.id
end
if result.updated_at != last_posted_at then
result.updated_at = last_posted_at
end
if result.created_at != created_at then
result.created_at = created_at
end
if result.bumped_at != bumped_at then
result.bumped_at = bumped_at
end
if result.last_posted_at != last_posted_at then
result.last_posted_at = last_posted_at
end
if result.excerpt != excerpt then
result.excerpt = excerpt
end
if result.changed.length > 0 then
result.save!
puts result.slug
post = result.first_post
post.created_at = created_at
post.updated_at = result.updated_at
post.baked_at = result.bumped_at
post.last_version_at = result.last_posted_at
post.user_id = result.user_id
post.last_editor_id = result.user_id
post.raw = result.excerpt
post.save!
puts post
#puts user.id
end
end
puts "get_topic.result.slug:#{result.slug}"
return result
end
Before the code creates a topic it searches for the title.
title = title.gsub(/ /,' ') result = Topic.find_by_title(title)
If a nil result occurs it then creates a topic.
It works for most topics, however, for one title it’s throwing ActiveRecord::RecordInvalid: Validation failed: Title has already been used when the topic is being saved.
The valiation settings for topic.title is
validates :title, if: Proc.new { |t| t.new_record? || t.title_changed? },
presence: true,
topic_title_length: true,
censored_words: true,
quality_title: { unless: :private_message? },
max_emojis: true,
unique_among: { unless: Proc.new { |t| (SiteSetting.allow_duplicate_topic_titles? || t.private_message?) },
message: :has_already_been_used,
allow_blank: true,
case_sensitive: false,
collection: Proc.new { Topic.listable_topics } }
I’ve checked in the console using Topic.find_by_title(‘title’). No topics exist with the title. I checked using Topic.find_by_sql("select * from topics where title like ‘start of title%’). No topics exist with the first word of the title.
What other checks can done … what could be causing the error saying the topic title has already been used when there are no topics found with that title?
Further information
I tried turning on allowing duplicate titles. The code ran through without an error yet there were no topics with the title that was causing the error after the import. Feels like the error message is wrong.
ruby discourse
|
show 2 more comments
I have a ruby function that creates a discourse topic if the title is not found.
def get_topic(user,title,created_at,bumped_at,last_posted_at,excerpt)
title = title.gsub(/ /,' ')
puts "get_topic.title:'#{title}' user.id:#{user.id} created_at:#{created_at} last_posted_at:#{last_posted_at} excerpt:#{excerpt}"
result = Topic.find_by_title(title)
if result == nil then
result = Topic.create
result.title = title
result.fancy_title = title
result.user_id = user.id
result.last_post_user_id = user.id
result.updated_at = last_posted_at
result.created_at = created_at
result.bumped_at = bumped_at
result.last_posted_at = last_posted_at
result.excerpt = excerpt
result.save!
else
puts result
puts created_at,bumped_at,last_posted_at
if result.user_id != user.id then
result.user_id = user.id
result.last_post_user_id = user.id
end
if result.updated_at != last_posted_at then
result.updated_at = last_posted_at
end
if result.created_at != created_at then
result.created_at = created_at
end
if result.bumped_at != bumped_at then
result.bumped_at = bumped_at
end
if result.last_posted_at != last_posted_at then
result.last_posted_at = last_posted_at
end
if result.excerpt != excerpt then
result.excerpt = excerpt
end
if result.changed.length > 0 then
result.save!
puts result.slug
post = result.first_post
post.created_at = created_at
post.updated_at = result.updated_at
post.baked_at = result.bumped_at
post.last_version_at = result.last_posted_at
post.user_id = result.user_id
post.last_editor_id = result.user_id
post.raw = result.excerpt
post.save!
puts post
#puts user.id
end
end
puts "get_topic.result.slug:#{result.slug}"
return result
end
Before the code creates a topic it searches for the title.
title = title.gsub(/ /,' ') result = Topic.find_by_title(title)
If a nil result occurs it then creates a topic.
It works for most topics, however, for one title it’s throwing ActiveRecord::RecordInvalid: Validation failed: Title has already been used when the topic is being saved.
The valiation settings for topic.title is
validates :title, if: Proc.new { |t| t.new_record? || t.title_changed? },
presence: true,
topic_title_length: true,
censored_words: true,
quality_title: { unless: :private_message? },
max_emojis: true,
unique_among: { unless: Proc.new { |t| (SiteSetting.allow_duplicate_topic_titles? || t.private_message?) },
message: :has_already_been_used,
allow_blank: true,
case_sensitive: false,
collection: Proc.new { Topic.listable_topics } }
I’ve checked in the console using Topic.find_by_title(‘title’). No topics exist with the title. I checked using Topic.find_by_sql("select * from topics where title like ‘start of title%’). No topics exist with the first word of the title.
What other checks can done … what could be causing the error saying the topic title has already been used when there are no topics found with that title?
Further information
I tried turning on allowing duplicate titles. The code ran through without an error yet there were no topics with the title that was causing the error after the import. Feels like the error message is wrong.
ruby discourse
Is the validation in the Topic class custom or simplyuniqueness: true
? Could you have titles in your DB that are equivalent to your search term after stripping whitespace, but not before?
– rwold
Jan 2 at 23:31
The validation is custom. Adding to question. The process of saving a Topic instance removes whitespace, including double spaces within the title.
– Keith John Hutchison
Jan 2 at 23:37
I'm not familiar withunique_among
, is that a custom validation? Some of your others appear to be. If so, please provide the code for that too.
– rwold
Jan 3 at 0:11
1
Does the title you're checking contain any single or double quotes, or special characters? Can you share the title you're validating against? Also, isfind_by_title
just the older syntax (e.g. equivalent toTopic.find_by(title: title)
, or is it a custom method on yourTitle
model?
– Jay Dorsey
Jan 3 at 2:37
1
@JayDorsey Discovered it was a case issue after a lead from Jay Pfaffman on meta.discourse.com. The title started with a lower case letter. When it was saved it the first letter was upper case.
– Keith John Hutchison
Jan 3 at 2:54
|
show 2 more comments
I have a ruby function that creates a discourse topic if the title is not found.
def get_topic(user,title,created_at,bumped_at,last_posted_at,excerpt)
title = title.gsub(/ /,' ')
puts "get_topic.title:'#{title}' user.id:#{user.id} created_at:#{created_at} last_posted_at:#{last_posted_at} excerpt:#{excerpt}"
result = Topic.find_by_title(title)
if result == nil then
result = Topic.create
result.title = title
result.fancy_title = title
result.user_id = user.id
result.last_post_user_id = user.id
result.updated_at = last_posted_at
result.created_at = created_at
result.bumped_at = bumped_at
result.last_posted_at = last_posted_at
result.excerpt = excerpt
result.save!
else
puts result
puts created_at,bumped_at,last_posted_at
if result.user_id != user.id then
result.user_id = user.id
result.last_post_user_id = user.id
end
if result.updated_at != last_posted_at then
result.updated_at = last_posted_at
end
if result.created_at != created_at then
result.created_at = created_at
end
if result.bumped_at != bumped_at then
result.bumped_at = bumped_at
end
if result.last_posted_at != last_posted_at then
result.last_posted_at = last_posted_at
end
if result.excerpt != excerpt then
result.excerpt = excerpt
end
if result.changed.length > 0 then
result.save!
puts result.slug
post = result.first_post
post.created_at = created_at
post.updated_at = result.updated_at
post.baked_at = result.bumped_at
post.last_version_at = result.last_posted_at
post.user_id = result.user_id
post.last_editor_id = result.user_id
post.raw = result.excerpt
post.save!
puts post
#puts user.id
end
end
puts "get_topic.result.slug:#{result.slug}"
return result
end
Before the code creates a topic it searches for the title.
title = title.gsub(/ /,' ') result = Topic.find_by_title(title)
If a nil result occurs it then creates a topic.
It works for most topics, however, for one title it’s throwing ActiveRecord::RecordInvalid: Validation failed: Title has already been used when the topic is being saved.
The valiation settings for topic.title is
validates :title, if: Proc.new { |t| t.new_record? || t.title_changed? },
presence: true,
topic_title_length: true,
censored_words: true,
quality_title: { unless: :private_message? },
max_emojis: true,
unique_among: { unless: Proc.new { |t| (SiteSetting.allow_duplicate_topic_titles? || t.private_message?) },
message: :has_already_been_used,
allow_blank: true,
case_sensitive: false,
collection: Proc.new { Topic.listable_topics } }
I’ve checked in the console using Topic.find_by_title(‘title’). No topics exist with the title. I checked using Topic.find_by_sql("select * from topics where title like ‘start of title%’). No topics exist with the first word of the title.
What other checks can done … what could be causing the error saying the topic title has already been used when there are no topics found with that title?
Further information
I tried turning on allowing duplicate titles. The code ran through without an error yet there were no topics with the title that was causing the error after the import. Feels like the error message is wrong.
ruby discourse
I have a ruby function that creates a discourse topic if the title is not found.
def get_topic(user,title,created_at,bumped_at,last_posted_at,excerpt)
title = title.gsub(/ /,' ')
puts "get_topic.title:'#{title}' user.id:#{user.id} created_at:#{created_at} last_posted_at:#{last_posted_at} excerpt:#{excerpt}"
result = Topic.find_by_title(title)
if result == nil then
result = Topic.create
result.title = title
result.fancy_title = title
result.user_id = user.id
result.last_post_user_id = user.id
result.updated_at = last_posted_at
result.created_at = created_at
result.bumped_at = bumped_at
result.last_posted_at = last_posted_at
result.excerpt = excerpt
result.save!
else
puts result
puts created_at,bumped_at,last_posted_at
if result.user_id != user.id then
result.user_id = user.id
result.last_post_user_id = user.id
end
if result.updated_at != last_posted_at then
result.updated_at = last_posted_at
end
if result.created_at != created_at then
result.created_at = created_at
end
if result.bumped_at != bumped_at then
result.bumped_at = bumped_at
end
if result.last_posted_at != last_posted_at then
result.last_posted_at = last_posted_at
end
if result.excerpt != excerpt then
result.excerpt = excerpt
end
if result.changed.length > 0 then
result.save!
puts result.slug
post = result.first_post
post.created_at = created_at
post.updated_at = result.updated_at
post.baked_at = result.bumped_at
post.last_version_at = result.last_posted_at
post.user_id = result.user_id
post.last_editor_id = result.user_id
post.raw = result.excerpt
post.save!
puts post
#puts user.id
end
end
puts "get_topic.result.slug:#{result.slug}"
return result
end
Before the code creates a topic it searches for the title.
title = title.gsub(/ /,' ') result = Topic.find_by_title(title)
If a nil result occurs it then creates a topic.
It works for most topics, however, for one title it’s throwing ActiveRecord::RecordInvalid: Validation failed: Title has already been used when the topic is being saved.
The valiation settings for topic.title is
validates :title, if: Proc.new { |t| t.new_record? || t.title_changed? },
presence: true,
topic_title_length: true,
censored_words: true,
quality_title: { unless: :private_message? },
max_emojis: true,
unique_among: { unless: Proc.new { |t| (SiteSetting.allow_duplicate_topic_titles? || t.private_message?) },
message: :has_already_been_used,
allow_blank: true,
case_sensitive: false,
collection: Proc.new { Topic.listable_topics } }
I’ve checked in the console using Topic.find_by_title(‘title’). No topics exist with the title. I checked using Topic.find_by_sql("select * from topics where title like ‘start of title%’). No topics exist with the first word of the title.
What other checks can done … what could be causing the error saying the topic title has already been used when there are no topics found with that title?
Further information
I tried turning on allowing duplicate titles. The code ran through without an error yet there were no topics with the title that was causing the error after the import. Feels like the error message is wrong.
ruby discourse
ruby discourse
edited Jan 3 at 0:28
Keith John Hutchison
asked Jan 2 at 23:21
Keith John HutchisonKeith John Hutchison
2,66442733
2,66442733
Is the validation in the Topic class custom or simplyuniqueness: true
? Could you have titles in your DB that are equivalent to your search term after stripping whitespace, but not before?
– rwold
Jan 2 at 23:31
The validation is custom. Adding to question. The process of saving a Topic instance removes whitespace, including double spaces within the title.
– Keith John Hutchison
Jan 2 at 23:37
I'm not familiar withunique_among
, is that a custom validation? Some of your others appear to be. If so, please provide the code for that too.
– rwold
Jan 3 at 0:11
1
Does the title you're checking contain any single or double quotes, or special characters? Can you share the title you're validating against? Also, isfind_by_title
just the older syntax (e.g. equivalent toTopic.find_by(title: title)
, or is it a custom method on yourTitle
model?
– Jay Dorsey
Jan 3 at 2:37
1
@JayDorsey Discovered it was a case issue after a lead from Jay Pfaffman on meta.discourse.com. The title started with a lower case letter. When it was saved it the first letter was upper case.
– Keith John Hutchison
Jan 3 at 2:54
|
show 2 more comments
Is the validation in the Topic class custom or simplyuniqueness: true
? Could you have titles in your DB that are equivalent to your search term after stripping whitespace, but not before?
– rwold
Jan 2 at 23:31
The validation is custom. Adding to question. The process of saving a Topic instance removes whitespace, including double spaces within the title.
– Keith John Hutchison
Jan 2 at 23:37
I'm not familiar withunique_among
, is that a custom validation? Some of your others appear to be. If so, please provide the code for that too.
– rwold
Jan 3 at 0:11
1
Does the title you're checking contain any single or double quotes, or special characters? Can you share the title you're validating against? Also, isfind_by_title
just the older syntax (e.g. equivalent toTopic.find_by(title: title)
, or is it a custom method on yourTitle
model?
– Jay Dorsey
Jan 3 at 2:37
1
@JayDorsey Discovered it was a case issue after a lead from Jay Pfaffman on meta.discourse.com. The title started with a lower case letter. When it was saved it the first letter was upper case.
– Keith John Hutchison
Jan 3 at 2:54
Is the validation in the Topic class custom or simply
uniqueness: true
? Could you have titles in your DB that are equivalent to your search term after stripping whitespace, but not before?– rwold
Jan 2 at 23:31
Is the validation in the Topic class custom or simply
uniqueness: true
? Could you have titles in your DB that are equivalent to your search term after stripping whitespace, but not before?– rwold
Jan 2 at 23:31
The validation is custom. Adding to question. The process of saving a Topic instance removes whitespace, including double spaces within the title.
– Keith John Hutchison
Jan 2 at 23:37
The validation is custom. Adding to question. The process of saving a Topic instance removes whitespace, including double spaces within the title.
– Keith John Hutchison
Jan 2 at 23:37
I'm not familiar with
unique_among
, is that a custom validation? Some of your others appear to be. If so, please provide the code for that too.– rwold
Jan 3 at 0:11
I'm not familiar with
unique_among
, is that a custom validation? Some of your others appear to be. If so, please provide the code for that too.– rwold
Jan 3 at 0:11
1
1
Does the title you're checking contain any single or double quotes, or special characters? Can you share the title you're validating against? Also, is
find_by_title
just the older syntax (e.g. equivalent to Topic.find_by(title: title)
, or is it a custom method on your Title
model?– Jay Dorsey
Jan 3 at 2:37
Does the title you're checking contain any single or double quotes, or special characters? Can you share the title you're validating against? Also, is
find_by_title
just the older syntax (e.g. equivalent to Topic.find_by(title: title)
, or is it a custom method on your Title
model?– Jay Dorsey
Jan 3 at 2:37
1
1
@JayDorsey Discovered it was a case issue after a lead from Jay Pfaffman on meta.discourse.com. The title started with a lower case letter. When it was saved it the first letter was upper case.
– Keith John Hutchison
Jan 3 at 2:54
@JayDorsey Discovered it was a case issue after a lead from Jay Pfaffman on meta.discourse.com. The title started with a lower case letter. When it was saved it the first letter was upper case.
– Keith John Hutchison
Jan 3 at 2:54
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014464%2fchecking-for-a-discourse-topic-title%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014464%2fchecking-for-a-discourse-topic-title%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is the validation in the Topic class custom or simply
uniqueness: true
? Could you have titles in your DB that are equivalent to your search term after stripping whitespace, but not before?– rwold
Jan 2 at 23:31
The validation is custom. Adding to question. The process of saving a Topic instance removes whitespace, including double spaces within the title.
– Keith John Hutchison
Jan 2 at 23:37
I'm not familiar with
unique_among
, is that a custom validation? Some of your others appear to be. If so, please provide the code for that too.– rwold
Jan 3 at 0:11
1
Does the title you're checking contain any single or double quotes, or special characters? Can you share the title you're validating against? Also, is
find_by_title
just the older syntax (e.g. equivalent toTopic.find_by(title: title)
, or is it a custom method on yourTitle
model?– Jay Dorsey
Jan 3 at 2:37
1
@JayDorsey Discovered it was a case issue after a lead from Jay Pfaffman on meta.discourse.com. The title started with a lower case letter. When it was saved it the first letter was upper case.
– Keith John Hutchison
Jan 3 at 2:54