我在Rails 5项目中设置了狮身人面像搜索引擎,它破坏了Rails belongs_to内置验证。示例:
class Post < ApplicationRecord
belongs_to :user
end
class Article < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :posts
has_many :articles
end早些时候,当我创建了一个新的Post并且没有在Post的new表单中设置User (作为父级)时,Rails触发了验证错误:
1 error prohibited this post from being saved:
User must exist现在,在添加Shpinx之后,这个验证就不再发生了(在Post创建时)。使用空的Post字段静默地创建一个新的user_id。
同样的问题现在也发生在Article模型上--它发生在任何具有belongs_to关联的模型上。
如何解决这个问题?当然,我可以简单地将validates :user, presence: true添加到Post和文章模型中--但我不喜欢这样笨拙的解决方案。
注意:如果我在我的gem 'thinking-sphinx', '~> 3.2.0'中注释掉了Gemfile行,那么所描述的问题就会消失。
发布于 2016-08-17 03:59:41
这确实是一个思考的狮身人面像虫,我有刚修好。
您可以通过在Gemfile中使用以下代码来使用最新的代码:
gem 'thinking-sphinx', '~> 3.2.0',
:git => 'git://github.com/pat/thinking-sphinx.git',
:branch => 'develop',
:ref => '3138fea725'https://stackoverflow.com/questions/38890554
复制相似问题