【问题标题】:Thinking-spinx breaks belongs_to built-in validationThinking-spinx 打破 belongs_to 内置验证
【发布时间】:2016-08-11 07:58:44
【问题描述】:
  • 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

我在我的 Rails 5 项目中设置了 Sphinx 搜索引擎,它破坏了 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 并且没有在Postnew 表单中设置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 和 Article 模型中 - 但我不喜欢这种笨拙的解决方案。

注意:如果我在 Gemfile 中注释掉 gem 'thinking-sphinx', '~&gt; 3.2.0' 行 - 所描述的问题就会消失。

【问题讨论】:

    标签: ruby-on-rails postgresql thinking-sphinx


    【解决方案1】:

    这确实是一个 Thinking Sphinx 错误,我已经 just fixed

    您可以通过在您的 Gemfile 中包含此代码来使用最新的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多