Получение ошибки StackLevel при сохранении хештегов в приложении Rails
У меня есть приложение Rails, которое позволяет добавлять хэштеги к разному контенту. Я получаю слишком глубокую ошибку уровня стека при попытке сохранить запись с хэштегом.
ошибка
Failure/Error: hashtag = Hashtag.find_by(name: hashtag_name)
SystemStackError:
stack level too deep
# ./app/models/concerns/hashtaggable_item.rb:28:in `block in parse_hashtags'
# ./app/models/concerns/hashtaggable_item.rb:27:in `each'
# ./app/models/concerns/hashtaggable_item.rb:27:in `parse_hashtags'
# ./app/models/concerns/hashtaggable_item.rb:42:in `update_display_body'
# ./app/models/concerns/hashtaggable_item.rb:42:in `update_display_body'
# ./app/models/concerns/hashtaggable_item.rb:42:in `update_display_body'
параметры
{
"action": "create",
"commit": "Post",
"controller": "discussions",
"discussion": {
"body": "Thank go it's #Friday",
"state_id": "35"
}
}
приложение / модели / проблемы / hashtaggable_item.erb
module HashtaggableItem
extend ActiveSupport::Concern
included do
# View Helpers
require 'link_thumbnailer'
include ActionView::Helpers
attr_accessor :output_buffer
include Twitter::TwitterText::Autolink
include Twitter::TwitterText::Extractor
# Relationships
has_many :hashtaggable_hashtags, as: :hashtaggable, dependent: :destroy_async
has_many :hashtags, through: :hashtaggable_hashtags
belongs_to :shared_link, optional: true
# Callbacks
after_create :parse_hashtags
before_update :parse_hashtags
after_save :update_display_body
private
def parse_hashtags
extract_hashtags(body).uniq.each do |hashtag_name|
hashtag = Hashtag.find_by(name: hashtag_name)
hashtags << (hashtag || Hashtag.create(name: hashtag_name)) unless hashtags.include? hashtag
end
end
def update_display_body
d_body = strip_tags(body)
d_body = auto_link(d_body, $TWITTER_TEXT_CONFIG)
if url = extract_urls(body).first
link = SharedLink.where(url: url).first_or_create(link_attributes(url))
update(shared_link_id: link.id)
end
update(display_body: sanitize(d_body))
end
def link_attributes(url)
lt = LinkThumbnailer.generate(url)
{ url: url, title: lt.title, description: lt.description, img_src: lt.images.first.try(:src) }
end
end
end
приложение / модели / обсуждение.rb
class Discussion < Content
default_scope -> { where(content_type: :discussion) }
end
приложение / модели / content.rb
class Content < ApplicationRecord
include HashtaggableItem
include PgSearch::Model
searchkick word_start: %i[body title state state_slug user], callbacks: :async
def search_data
{
upvotes_count: upvotes_count,
body: body,
title: title,
state: state_name,
state_slug: state_slug,
user: user_full_name,
created_at: created_at,
updated_at: updated_at
}
end
acts_as_paranoid
belongs_to :user
delegate :full_name, to: :user, allow_nil: true
belongs_to :politician, optional: true, class_name: 'LegiscanModel::Politician'
belongs_to :shared_link, optional: true
belongs_to :state, class_name: 'LegiscanModel::State', foreign_key: :legiscan_model_state_id
delegate :name, :abbr, :slug, to: :state, prefix: true, allow_nil: true
has_many :upvotes, as: :upvotable, dependent: :destroy_async, counter_cache: true
has_many :comments, as: :commentable, dependent: :destroy_async, counter_cache: true
validates :body, presence: true
enum content_type: { question: 0, discussion: 1, bill: 2 }
delegate :display_name, :full_name, :title, to: :user, prefix: true
end
приложение / контроллеры / обсуждения_controller.rb
def create
respond_to do |format|
format.js do
@discussion.user = current_user
# byebug
@discussion.save!
render body: html_content, status: :ok
end
end
end