Project

eav_model

0.0
No release in over a year
Allow usage of the EAV model in your Rails application
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

~> 7.0
 Project Readme

EAV Model

EAV Model is a Rails gem for adding an Entity-Attribute-Value (EAV) model to your Rails application.

Installation

Add this line to your application's Gemfile:

gem 'eav_model'

And then execute:

bundle install

Or install it yourself with the following command:

gem install eav_model

Usage

In your Rails model:

class Product < ApplicationRecord
  include EavModel
  eav_model_for :attributes, :values
end

This will set up has_many :attributes and has_many :values, through: :attributes associations on the Product model.

Next, run the following command to create the necessary migration:

rails generate eav_model:migration

And then migrate the database:

rails db:migrate

Now, you can use the attributes association to add attributes to your Product model. Each attribute can have an IntegerValue, StringValue, or BooleanValue.

Here is an example:

product = Product.create(name: 'Smartphone')

# Create an attribute
attribute = product.attributes.create(name: 'Color')

# Create a string value for the attribute
StringValue.create(value: 'Black', attribute: attribute)

# Fetch the values for an attribute
attribute.string_values # Returns a collection of string values
attribute.integer_values # Returns a collection of integer values
attribute.boolean_values # Returns a collection of boolean values

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DerekCrosson/eav_model. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the EAV Model project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.