【问题标题】:Rails has_many :through validation issueRails has_many:通过验证问题
【发布时间】:2015-03-23 14:15:35
【问题描述】:

我整个星期都在使用 Rails has_many 进行战斗。最初我在获取collection_select form helper to save 时遇到问题。

我最终得到了它的工作,并开始尝试获取 json 发布请求以支持添加新作者。我在使用现在正在运行的表单创建的请求参数之后对 json 进行了建模。我第一次使用这些参数发出请求时它就起作用了:

{
    "author": {
        "name": "Author Name",
        "post_ids": [
            "1", "2"
        ]
    }
}

我开始测试我的验证,遇到了一个问题,如果发送的 post_id 在数据库中不存在Rails 将轰炸@author.new 方法:

请求:

{
    "author": {
        "name": "Author Name",
        "post_ids": [
            "23"
        ]
    }
}

错误:

ActiveRecord::RecordNotFound (Couldn't find Post with 'id'=23):
  app/controllers/authors_controller.rb:32:in `create'

控制器

  def create
    @author = Author.new(author_params)

    respond_to do |format|
      if @author.save
        format.html { redirect_to @author, notice: 'Author was successfully created.' }
        format.json { render :show, status: :created, location: @author }
      else
        format.html { render :new }
        format.json { render json: @author.errors, status: :unprocessable_entity }
      end
    end
  end

这条线上正在轰炸

@author = Author.new(author_params)

我尝试使用验证来确保 id 存在,但它在验证之前就出错了。似乎 rails 正在以新方法创建关联。

我怎样才能捕捉到这个?我在 Author.new 调用之前写了一个检查,以确保发送的 author_ids 存在,但如果 rails 提供了这种能力,我'希望能够使用内置功能来捕获它并将其与其他验证消息一起发回。

型号:

class Author < ActiveRecord::Base
  has_many :post_authors
  has_many :posts, :through => :post_authors
  accepts_nested_attributes_for :post_authors
end

class Post < ActiveRecord::Base
end

class PostAuthor < ActiveRecord::Base
  belongs_to :post
  belongs_to :author
end

架构

ActiveRecord::Schema.define(version: 20150120190715) do

  create_table "authors", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "post_authors", force: :cascade do |t|
    t.integer  "post_id"
    t.integer  "author_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

感谢您的帮助。

验证尝试

我在所有 3 个模型中都进行了以下验证。我添加了断点和日志消息。在错误发生之前它们都没有命中,所以在这一点上,我无法进行验证,甚至能够进行检查。

class Author < ActiveRecord::Base
  has_many :post_authors
  has_many :posts, :through => :post_authors
  accepts_nested_attributes_for :post_authors

  validate :post_exists

  def post_exists
    Rails.logger.debug("Validate")
  end
end

【问题讨论】:

  • 显示您尝试过的验证。
  • 不确定这是否可行,但请尝试将 has_many :post_authors 更改为 has_many :post_authors, inverse_of: :author
  • 我尝试了 inverse_of,但不幸的是它没有任何区别。
  • @Nobita,我最初尝试编写一些东西来检查发送的 id 与表中的 id,但我意识到验证永远不会触发。我在所有 3 个模型中都进行了此验证,以及日志消息和 Ruby 中的断点,并且验证在错误之前从未触发。 class Author &lt; ActiveRecord::Base has_many :post_authors has_many :posts, :through =&gt; :post_authors accepts_nested_attributes_for :post_authors validate :post_exists def post_exists Rails.logger.debug("Validate") end end
  • 将其添加到上面的帖子中以便于阅读。谢谢。

标签: ruby-on-rails json validation has-many-through


【解决方案1】:

根据rails guide,只有在调用以下方法时才会触发验证:

  • 创建
  • 创建!
  • 保存
  • 保存!
  • 更新
  • 更新!

所以我认为你最好的机会是拯救你的控制器中的ActiveRecord::RecordNotFound,正如这个问题中所解释的那样:

how to handle ActiveRecord::RecordNotFound in rails controller?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 2010-12-15
    • 2023-01-29
    • 1970-01-01
    相关资源
    最近更新 更多