[英]How can I extend ActiveRecord::Associations to a frozen_record in rails
I'm using the Frozen Record gem in Rails to upload a YAML file containing some fixed questions for my app. 我在Rails中使用“ 冻结记录” gem上传了一个YAML文件,其中包含有关我的应用程序的一些固定问题。 Once they're uploaded I want to use a SaleQualifier model to grab each question, associate it with an answer, and then use it as a state machine to move through a question tree.
上载它们之后,我想使用SaleQualifier模型来抓取每个问题,将其与答案相关联,然后将其用作状态机来遍历问题树。
I've got the Frozen Record gem working and the YAML file uploads just fine. 我的Frozen Record gem可以正常工作了,YAML文件上传就很好了。 When I tried associating the Model with a new Model (SaleQualifier) I got
'method_missing': undefined method 'belongs_to' for Question:Class (NoMethodError)
当我尝试将模型与新模型(SaleQualifier)相关联时,出现
'method_missing': undefined method 'belongs_to' for Question:Class (NoMethodError)
As a result I added the include ActiveRecord::Associations components in order to let me associate the new record with my SaleQualifier Question belongs_to :sale_qualifier
- but this then throws: 结果,我添加了include ActiveRecord :: Associations组件,以使我可以将新记录与我的SaleQualifier
Question belongs_to :sale_qualifier
-但这会引发:
'method_missing': undefined method 'dangerous_attribute_method?' for Question:Class (NoMethodError)
According to my search, this error is thrown when I've already declared the method beforehand. 根据我的搜索,当我事先声明该方法时,将引发此错误。 I don't know where this is defined, but from the Frozen_Record gem files I can see they set up a
FrozenRecord
with the following: 我不知道它的定义位置,但是从Frozen_Record gem文件中,我可以看到它们使用以下内容设置了
FrozenRecord
:
module FrozenRecord
class Base
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
end
end
I'm starting to think using this gem might be overkill, and perhaps I should just load my questions into a normal ActiveRecord::Base
model, but I like the FrozenRecord idea, as that's exactly what I'm trying to achieve. 我开始认为使用此gem可能会过大,也许我应该将问题加载到正常的
ActiveRecord::Base
模型中,但是我喜欢FrozenRecord的想法,因为这正是我要实现的目标。
My code: 我的代码:
class Question < FrozenRecord::Base
include ActiveModel::Validations
include ActiveRecord::Associations
validates :next_question_id_yes, :question_text, :answer_type, presence: true
belongs_to :sale_qualifier
self.base_path = 'config/initializers/'
end
SaleQualifier: 销售资格:
class SaleQualifier < ActiveRecord::Base
has_many :questions
end
Can anyone help me unpick the mess I seem to have dug myself into? 有人可以帮我解开我似乎自己陷入的混乱吗? Maybe I ought to just dig out the YAML upload functions from FrozenRecord and dump them into my Question model without using the gem.
也许我应该只是从FrozenRecord中挖掘出YAML上传功能,然后将它们转储到我的Question模型中而不使用gem。
So in the end, Frozen_Record wasn't really adding much to my app it seems. 因此,最终,Frozen_Record似乎并没有真正增加我的应用程序。 Given the files are loaded from YAML and there's no Controller, I presume there's no way for the Question records to be updated, unless someone has already compromised my DB and inserted their own questions.yml file.
给定文件是从YAML加载的,并且没有控制器,我想没有办法更新Question记录,除非有人已经破坏了我的数据库并插入了自己的questions.yml文件。
As such I just changed the Question model as follows to load the YAML and insert it into the Database as new Question records: 因此,我只是如下更改了Question模型,以加载YAML并将其作为新Question记录插入数据库中:
class Question < ActiveRecord::Base
validates :next_question_id_yes, :question_text, :answer_type, presence: true
belongs_to :sale_qualifier
File.open("#{Rails.root}/config/initializers/questions.yml", 'r') do |file|
YAML::load(file).each do |record|
Question.create(record)
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.