Is it possible to have multiple store attributes in a single model | Ram Laxman Yadav

Is it possible to have multiple store attributes in a single model

Yes. We can have multiple store attributes in a single table. Here is an example:

Create a model:

  • Run rails g model user
  • Change migration as follow:
    class CreateUsers < ActiveRecord::Migration[5.0]
      def change
        create_table :users do |t|
          t.text :store1
          t.text :store2
          t.text :store3
          t.timestamps
        end
      end
    end

Make store attributes inside the model

  • Add following lines inside the model:
    class User < ApplicationRecord
      store :store1,accessors: [:hello]
      store :store2,accessors: [:hello1]
      store :stroe3,accessors: [:hello2]
    end
    # Now wil be able to make multiple stores inside one table.
    # Make sure you have entered different accessors for each store. If you give same it will return nil

Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer