Thinking-spinx breaks belongs_to built-in validation

吃可爱长大的小学妹 提交于 2019-12-12 03:49:37

问题


  • Ubuntu 16.04.1 LTS
  • Rails 5.0.0
  • ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
  • gem 'mysql2', '~> 0.3.18', :platform => :ruby
  • gem 'thinking-sphinx', '~> 3.2.0'
  • PostgreSQL 9.5.3

I set up the Sphinx search engine in my Rails 5 project and it broke the Rails belongs_to built-in validation. Example:

class Post < ApplicationRecord
  belongs_to :user 
end

class Article < ApplicationRecord
  belongs_to :user 
end

class User < ApplicationRecord
  has_many :posts
  has_many :articles
end

Earlier when I created a new Post and did not set the User (as the parent) in the Post's new form, Rails fired the validation error:

1 error prohibited this post from being saved:
User must exist

Now after adding Shpinx this validation does not happen anymore (at the Post creation). A new Post is silently created with an empty user_id field.

Same problem happens now to the Article model too - it happens with whatever models which have the belongs_to association.

How to fix this problem? Of course, I could simply add validates :user, presence: true to both Post and Article models - but I don't like such clumsy solutions.

NOTE: If I comment out the gem 'thinking-sphinx', '~> 3.2.0' line in my Gemfile - the described problem disappears.


回答1:


This was indeed a Thinking Sphinx bug, which I've just fixed.

You can use the latest by having this code in your Gemfile:

gem 'thinking-sphinx', '~> 3.2.0',
  :git    => 'git://github.com/pat/thinking-sphinx.git',
  :branch => 'develop',
  :ref    => '3138fea725'


来源:https://stackoverflow.com/questions/38890554/thinking-spinx-breaks-belongs-to-built-in-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!