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

Posted by : on

Category : Rails

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

###



About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in