Ruby on rails 4 如何将嵌套模型中的动态hstore键列为白名单
我有以下关系:Ruby on rails 4 如何将嵌套模型中的动态hstore键列为白名单,ruby-on-rails-4,nested-attributes,strong-parameters,hstore,Ruby On Rails 4,Nested Attributes,Strong Parameters,Hstore,我有以下关系: class Applicant < ActiveRecord::Base has_many :answers accepts_nested_attributes_for :answers end class Answer < ActiveRecord::Base belongs_to :applicant end 谢谢你的帮助。UPD。尝试使用以下方法(在单独的文件中测试): 顺便说一句。与hstores合作有很多好处。以及一些关于使用的一般事项。UP
class Applicant < ActiveRecord::Base
has_many :answers
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :applicant
end
谢谢你的帮助。UPD。尝试使用以下方法(在单独的文件中测试):
顺便说一句。与hstores合作有很多好处。以及一些关于使用的一般事项。UPD。尝试使用以下方法(在单独的文件中测试):
顺便说一句。与hstores合作有很多好处。以及一些关于使用的一般事项。UPD。尝试使用以下方法(在单独的文件中测试):
顺便说一句。与hstores合作有很多好处。以及一些关于使用的一般事项。UPD。尝试使用以下方法(在单独的文件中测试):
顺便说一句。与hstores合作有很多好处。还有一些关于使用的一般性问题。看起来这两个示例在hstore列中都有可预测的键。此应用程序具有动态的用户定义密钥。如果我读错了,请告诉我。虽然这应该行得通,但不是rails的方式:看起来这两个示例在hstore列中都有可预测的键。此应用程序具有动态的用户定义密钥。如果我读错了,请告诉我。虽然这应该行得通,但不是rails的方式:看起来这两个示例在hstore列中都有可预测的键。此应用程序具有动态的用户定义密钥。如果我读错了,请告诉我。虽然这应该行得通,但不是rails的方式:看起来这两个示例在hstore列中都有可预测的键。此应用程序具有动态的用户定义密钥。如果我读错了,请告诉我。虽然这应该有效,但不是rails的方式:
def applicant_params
params.require(:applicant).permit(:answers_attributes: [:question_id, :id]).tap do |whitelisted|
whitelisted[:answers_attributes][:properties] = params[:applicant][:answers_attributes][:properties]
end
end
@params = ActionController::Parameters.new(
applicant: {answers_attributes: {
"0" => {question_id: 10, id: 110, properties: {a: "b", c: "d"}},
"1" => {question_id: 20, id: 120, properties: {m: "n", o: "p"}}
}})
def applicant_params
#properties should be [:a, :c, :m, :o]
properties = []
@params[:applicant][:answers_attributes].values.each do |answer|
properties |= answer[:properties].keys
end
@params.require(:applicant).permit(answers_attributes:
[:question_id, :id, properties: properties])
end