I'm using the Frozen Record gem in Rails to upload a YAML file containing some fixed questions for my app. 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.
I've got the Frozen Record gem working and the YAML file uploads just fine. When I tried associating the Model with a new Model (SaleQualifier) I got '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:
'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:
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.
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.
So in the end, Frozen_Record wasn't really adding much to my app it seems. 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.
As such I just changed the Question model as follows to load the YAML and insert it into the Database as new Question records:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.