P
P
Philipp2014-09-15 18:33:49
Ruby on Rails
Philipp, 2014-09-15 18:33:49

How to make virtual columns in ActiveRecord?

The thing is elementary. There is a table of products with a fixed set of columns, such as name and price.
These characteristics are present in all products. And there is, for example, color and size, or, even worse, some unique feature inherent only in certain products.
Different price lists will be loaded into the system, each of which will look like with a different set of fields, etc.
It is supposed to make the following structure

Product [id, brand, name, price]
Field [id, name, order]
Value [id, value]
Data [id, product_id, field_id, value_id]

This scheme has sufficient flexibility, but I have absolutely no idea how to implement it.
In terms of data import, I found roo, but then I got stuck.
Models as a whole I can imagine how to do, but how to put in and pull out data is weak.
What seems to be elementary in terms of JOIN at the SQL level, I do not understand how it works.
I've only seen Rails for a few days. I read the man , but I got even more confused.
We need either an article on this topic or just an explanation of how such things are done in rails. The lack of knowledge simply affects (a week of acquaintance with the language).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vsuhachev, 2014-09-15
@zoonman

I think it can be simplified

Product [id, brand, name, price]
Field [id, name, order]
Data [id, product_id, field_id, value]

Or can you have many values ​​for one field?
You can save data by creating objects and calling their save method. Or by immediately calling the create method

_
_ _, 2014-09-15
@AMar4enko

It all depends on what you will need to do with this data.
If it's just to store, then welcome to PostgreSQL and the json column type (at least write the hell out there).
If you need to search, then I would stay with PostgreSQL and hang ElasticSearch on top and search in it.
And all these EAVs are good only in the picture. When difficult requests begin - "floated".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question