# Dynamic Fields (attributes)

**URL:** <https://www.ruby-forum.com/t/dynamic-fields-attributes/173560>\
**Category:** Rails\
**Created:** [August 11, 2009, 1:29pm UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560 "2009-08-11T13:29:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 11, 2009, 1:29pm UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/1 "2009-08-11T13:29:32Z")

</div>

Hello!

I am looking for a plugin or at least the way how to realize dynamic  
fields in a model.

Why I need: I want to create a product catalog application, each type  
of product can have different set of fields (e.g. computer, mobile  
phone, vacume cleaner and etc.).  
Requirements:

1. Set of fields with types can be defined from admin panel foreach  
product type
2. Validation should be presented
3. It should be possible to sort and filter on this fields
4. It should be possible to auto-generate forms for products

All plugins that i found does not give me this functionality.

Who can advise me something or point to a plugin?

---

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 11, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/2 "2009-08-11T14:52:40Z")

</div>

I’m actually doing something like this right now. My example is a lot  
more complex because there need to be various depths and not every  
attribute is in fact an attribute, but some are a set of options, but  
basically I can point you in the right direction:

Your catalog consists of products, so you’ll have a model Product.  
They will have attributes, so you’ll have a model ProductAttribute.  
Then you’ll need a table that links them together. Name it anything  
you’d like, I chose ProductDefinition. Then, in the corresponding  
models do:

class Product \< ActiveRecord::Base  
has\_many :product\_definitions  
has\_many :product\_attributes, :through =\> :product\_definitions  
end

class ProductAttribute \< ActiveRecord::Base  
has\_many :product\_definitions  
has\_many :products, :through =\> :product\_definitons  
end

class ProductDefinition \< ActiveRecord::Base  
belongs\_to :product  
belongs\_to :product\_attribute  
end

Now you can use eager loading to define a product:

@product = Product.find(:first)

And do @product.attributes to get all the product’s attributes.

Hope this helps!

Kind regards,  
Jaap H.  
w. [http://www.relywebsolutions.nl](http://www.relywebsolutions.nl)

---

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 11, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/3 "2009-08-11T22:59:14Z")

</div>

Thank you Jaap!

I thought about it previousely, but one problem exists with with  
validation and strong types of attributes. As i understand  
ProductAttribute should have several number of fields for different  
types of values e.g. string, decimal, integer, boolean and etc. And  
there should be an accessor wich will give us value from field  
dependent on type wich was defined in ProductDefinition. Am I right?

May be we can collaborate over this problem?

---

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 12, 2009, 9:00am UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/4 "2009-08-12T09:00:22Z")

</div>

I think convert from string is not a good solution, because we will  
not be able to use WHERE and ORDER BY queries over attributes (e.g.  
ORDER BY string and number is strongly different).  
Also I think we need some kind of Query Builder e.g. :orderby =\> “#  
{attriDef.field}” or :condition=\> “#{attriDef.field} = ?”.  
“attriDef.field” should return a field name dependent on attribute  
type.

---

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 12, 2009, 10:31am UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/5 "2009-08-12T10:31:06Z")

</div>

> I think convert from string is not a good solution, because we will  
> not be able to use WHERE and ORDER BY queries over attributes (e.g.  
> ORDER BY string and number is strongly different).

Yup, that’s true. You can of course let Rails do part of the work for  
your database, but you don’t want to retrieve rows you don’t need of  
course, so it’s probably not the way to go.

> Also I think we need some kind of Query Builder e.g. :orderby =\> “#  
> {attriDef.field}” or :condition=\> “#{attriDef.field} = ?”.  
> “attriDef.field” should return a field name dependent on attribute  
> type.

Well, you know what you’re looking for, right? If you’re comparing an  
integer to values in your table, you know you’re looking for an  
integer. I’m pretty sure Rails can do that for you.

---

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 11, 2009, 11:44pm UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/6 "2009-08-11T23:44:54Z")

</div>

On 11 aug, 22:57, insect [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you Jaap!
> 
> I thought about it previousely, but one problem exists with with  
> validation and strong types of attributes. As i understand  
> ProductAttribute should have several number of fields for different  
> types of values e.g. string, decimal, integer, boolean and etc. And  
> there should be an accessor wich will give us value from field  
> dependent on type wich was defined in ProductDefinition. Am I right?

I have had a very long day and I now see that I may have  
misunderstood: you might not even need a many-to-many relation, am I  
right? There’s always the possibility of just using has\_many and  
belongs\_to between the product and attribute table. Especially if you  
want to be able to, for example, destroy a product and its attributes,  
you don’t want to care too much about double entries.

As for the string, integer, boolean etc.: that’s true. There are a few  
methods that come to mind and the first one is indeed creating fields  
for all these possibilities and just looking which field contains a  
value. You can evaluate whether it’s a string, a boolean or an integer  
using the field it’s in. However, I’m unsure whether that’s the most  
robust solution. You could also store every value as a string and  
convert it back in your model.

> May be we can collaborate over this problem?

Sure, you can always contact me directly, but I’m no expert yet, I’m  
probably as much in the learning process as you are and the Rails  
community you’ve found here is full of true experts. Let’s have my  
responses be evaluated by the community instead of taking them for  
truth. That way we can both learn.

---

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 26, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/7 "2009-08-26T18:54:49Z")

</div>

Hello,

No. I need a system where attributes of model can be defined from  
admin panel dynamically. So I should be able to add and remove fields  
of models.  
E.g. I have 2 kinds of products mobile phones and cars. Each type has  
own set of attributes. Also user should be able to sort and filter  
model by this fields.

---

<div class="post-metadata">

**Author:** ![insect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2acd7d/32.png) [@insect](https://www.ruby-forum.com/u/insect)\
**Post date:** [August 14, 2009, 2:52am UTC](https://www.ruby-forum.com/t/dynamic-fields-attributes/173560/8 "2009-08-14T02:52:02Z")

</div>

On Aug 11, 2009, at 11:43 AM, insect wrote:

> 1. Set of fields with types can be defined from admin panel foreach  
> product type
> 2. Validation should be presented
> 3. It should be possible to sort and filter on this fields
> 4. It should be possible to auto-generate forms for products
> 
> All plugins that i found does not give me this functionality.
> 
> Who can advise me something or point to a plugin?

I don’t know about autogeneration of forms, but it seems to me that  
what you want is a polymorphic association, where you have a model  
that contains common attributes, and specific models with a specified  
set of attributes.  
Does this answer your question ?

ngw
