从关联模型回调访问所有模型
Access all model from associated models callback
我有一个带有回调的问题模型:
class Question < ActiveRecord::Base
has_many :qtags
has_many :tags, through: :qtags
after_save { self.tags.find_or_create(self) }
end
这会将问题传递给 Tag,它应该从 tag_list
创建一些标签
在 tag.rb 中,我想访问所有标签以检查是否已经存在:
class Tag < ActiveRecord::Base
has_many :qtags
has_many :questions, through: :qtags
def self.find_or_create(question)
question.tags.destroy_all
question.tag_list.split(' ').each do |tag|
# Tag.where here can't access all the tags, just the associated ones.
if Tag.where(name: tag).exists? && question.tags.ids.exclude?(Tag.where(name: tag).first.id)
question.tags << Tag.where(name: tag)
elsif Tag.where(name: tag).blank?
question.tags << Tag.create(name: tag)
end
end
end
end
然而,这只会检查具有相关问题 ID 的标签。
所以,当我用 binding.pry 添加一个撬控制台时:
5: def self.find_or_create(question)
6: question.tags.destroy_all
=> 7: binding.pry
8: question.tag_list.split(' ').each do |tag|
9: if Tag.where(name: tag).exists? && question.tags.ids.exclude?(Tag.where(name: tag).first.id)
10: question.tags << Tag.where(name: tag)
11: elsif Tag.where(name: tag).blank?
12: question.tags << Tag.create(name: tag)
13: end
14: end
15: end
[1] pry(Tag)> Tag.all
Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "qtags" ON "tags"."id" = "qtags"."tag_id" WHERE "qtags"."question_id" = ? [["question_id", 11]]
它仅选择具有相关问题 ID 的标签。
我如何才能访问带有 Tag.all 或 Tag.where(..) 的所有标签,而不仅仅是那些具有来电者问题 ID 的标签?
我无法解决问题,但我是这样解决的:
我没有使用回调,而是从控制器调用了 find_or_create 方法,如下所示:
def create
Tag.find_or_create(@question) if @question.save
respond_with(@question)
end
编辑:
现在我知道错误是什么了:
after_save { self.tags.find_or_create(self) }
这应该是:
after_save { Tag.find_or_create(self) }
我有一个带有回调的问题模型:
class Question < ActiveRecord::Base
has_many :qtags
has_many :tags, through: :qtags
after_save { self.tags.find_or_create(self) }
end
这会将问题传递给 Tag,它应该从 tag_list
创建一些标签在 tag.rb 中,我想访问所有标签以检查是否已经存在:
class Tag < ActiveRecord::Base
has_many :qtags
has_many :questions, through: :qtags
def self.find_or_create(question)
question.tags.destroy_all
question.tag_list.split(' ').each do |tag|
# Tag.where here can't access all the tags, just the associated ones.
if Tag.where(name: tag).exists? && question.tags.ids.exclude?(Tag.where(name: tag).first.id)
question.tags << Tag.where(name: tag)
elsif Tag.where(name: tag).blank?
question.tags << Tag.create(name: tag)
end
end
end
end
然而,这只会检查具有相关问题 ID 的标签。
所以,当我用 binding.pry 添加一个撬控制台时:
5: def self.find_or_create(question)
6: question.tags.destroy_all
=> 7: binding.pry
8: question.tag_list.split(' ').each do |tag|
9: if Tag.where(name: tag).exists? && question.tags.ids.exclude?(Tag.where(name: tag).first.id)
10: question.tags << Tag.where(name: tag)
11: elsif Tag.where(name: tag).blank?
12: question.tags << Tag.create(name: tag)
13: end
14: end
15: end
[1] pry(Tag)> Tag.all
Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "qtags" ON "tags"."id" = "qtags"."tag_id" WHERE "qtags"."question_id" = ? [["question_id", 11]]
它仅选择具有相关问题 ID 的标签。
我如何才能访问带有 Tag.all 或 Tag.where(..) 的所有标签,而不仅仅是那些具有来电者问题 ID 的标签?
我无法解决问题,但我是这样解决的:
我没有使用回调,而是从控制器调用了 find_or_create 方法,如下所示:
def create
Tag.find_or_create(@question) if @question.save
respond_with(@question)
end
编辑:
现在我知道错误是什么了:
after_save { self.tags.find_or_create(self) }
这应该是:
after_save { Tag.find_or_create(self) }