Home > Back-end >  Rails 7 - Storing hash as additional fields
Rails 7 - Storing hash as additional fields

Time:10-31

I have a rails hash for ice_cube that is stored in a field called recurring that looks like this:

{:validations=>{:day=>[1, 2, 4]}, :rule_type=>"IceCube::WeeklyRule", :interval=>1, :week_start=>0}

I want to keep that but also store the day key in a field called days and the rule_type in a separate rule_type field. So I would have a DB column called days that would have 1,2,4 in it and a column called rule_type that would have "WeeklyRule" in it.

How do you reference the hash values on create of the model record?

CodePudding user response:

Just access the hash values then save fields:

recurring = {:validations=>{:day=>[1, 2, 4]}, :rule_type=>"IceCube::WeeklyRule", :interval=>1, :week_start=>0}

days = recurring[:validations][:day]
=> [1, 2, 4] 
rule_type = recurring[:rule_type] # or recurring[:rule_type].gsub('IceCube::', '') if you want only WeeklyRule
=> "IceCube::WeeklyRule" 
  • Related