0

My Dev Env

Rails 6.0.3.4

sqlite3

ruby 2.6.3

and a RoR n00B

My Model Goal has a few attributes like title ,created_on etc. Essentially standard types like text,date,boolean etc.

I have validations in place,I am able to read from DB, post from form etc.CRUD operations are fine.So far so good.

Now I want to add a new column called tracking_data which will be a type of hash like this

  {
    21-12-2020:true,
    22-12-2020:false,
    etc
    etc
  }

The idea is goal will have a column called tracking_data like above which is essentially a hash of { date:bool }. It simply records if on a particular date(key), the user has worked on the goal.True or false(value). Essentially the user will select a date, check/uncheck the status for the date in the form. I am using sqlite3 currently. As mentioned here from the official docs https://api.rubyonrails.org/classes/ActiveRecord/Store.html, I am trying to implement the same. What I have done so far, and which obviously is not correct is define something like

store :tracking_data, accessors: [ :date, :status] on the goal.rb model file. By virtue of the store wrapper, I get access to the date and status accessor methods on goal. Which I use to read inputs from the form. The form code looks like this

  <%= form.date_field :date %>
  <%= form.check_box :status %>

So my query is

How can I read the date input by the user from the form along with the status and set that as key/value in the tracking_data hash.

Basically I should not be passing the :date attribute in the store function, because that has to be input by the user.

Additional I am using the default Yaml serializer with store. Any help is appreciated and please let me know If I can clarify this further.

Thank you

1 Answer 1

1

If you define the column data type as jsonb, you can create or update records with something like

Goal.create!(tracking_data: { '21-12-2020': true })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.