强参数中的Rails不规则复数关联名称

时间:2019-01-08 18:11:33

标签: ruby-on-rails model-associations

我有一个“ LineOfBusiness”模型,该模型具有不规则的复数形式(“ lines_of_business”而不是“ line_of_businesses”)。

我已经建立了“ has_many through”关联,并进行了我认为是对不规则名称的必要调整,但是我不确定为什么必须在ID中使用ID的复数形式参数。如我所料,单数形式的“ user_ids”可以在参数中使用,但是我必须使用复数形式的“ lines_of_business_ids”才能使其正常工作。似乎有些小事情要解决了,但我无法弄清楚。

class Question < ApplicationRecord
  has_many :user_questions, dependent: :destroy
  has_many :users, through: :user_questions
  has_many :line_of_business_questions, dependent: :destroy
  has_many :lines_of_business, through: :line_of_business_questions, :source => :line_of_business
end

class User < ApplicationRecord
  has_many :user_questions, dependent: :destroy
  has_many :questions, through: :user_questions
end

class LineOfBusiness < ApplicationRecord
  self.table_name = "lines_of_business"
  has_many :line_of_business_questions, dependent: :destroy
  has_many :questions, through: :line_of_business_questions
end

def question_params
  params.require(:question).permit(:name, :input_type, user_ids: [], lines_of_business_ids: [])
end

0 个答案:

没有答案