# Has\_many with model name as foreign key

**URL:** <https://www.ruby-forum.com/t/has-many-with-model-name-as-foreign-key/233595>\
**Category:** Rails\
**Created:** [December 27, 2014, 4:16pm UTC](https://www.ruby-forum.com/t/has-many-with-model-name-as-foreign-key/233595 "2014-12-27T16:16:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [December 27, 2014, 4:16pm UTC](https://www.ruby-forum.com/t/has-many-with-model-name-as-foreign-key/233595/1 "2014-12-27T16:16:58Z")

</div>

Hi,

It’s possible to do this :

models:

- product has\_many attributes
- user has\_many attributes
- other\_model has\_many attributes

model attribute belongs to

- product , but the foreign\_key is not product\_id but the mode name  
(Product) stored in table attributes(id, name, model)
- user , but the foreign\_key is not product\_id but the mode name  
(Product) stored in table attributes(id, name, model)

I want associate the attributes model with whole model and not with  
single  
record.

thx

---

<div class="post-metadata">

**Author:** ![fmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a698b9/32.png) [@fmh](https://www.ruby-forum.com/u/fmh)\
**Post date:** [December 27, 2014, 5:01pm UTC](https://www.ruby-forum.com/t/has-many-with-model-name-as-foreign-key/233595/2 "2014-12-27T17:01:30Z")

</div>

Hi,

Fistly, dont create a model with the name “Attribute”. Some of the rails  
core methods uses “attributes” . Use something like “custom\_attributes”  
.

Next, to make a model belong\_to different models on the same  
foreign\_key,  
Make sure you use polymorphic-associations  
[http://guides.rubyonrails.org/association\_basics.html#polymorphic-associations](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations)  
in rails.

---

<div class="post-metadata">

**Author:** ![fmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a698b9/32.png) [@fmh](https://www.ruby-forum.com/u/fmh)\
**Post date:** [December 27, 2014, 5:20pm UTC](https://www.ruby-forum.com/t/has-many-with-model-name-as-foreign-key/233595/3 "2014-12-27T17:20:40Z")

</div>

thx,

but with polymorphic associations, i always need to set a foreign key  
column,

my need is to set only the column type (model name)…

class Picture \< ActiveRecord::Base  
belongs\_to :imageable, polymorphic: true

#table pictures (id, name, imageable\_id, imageable\_type)

# how use it without imageable\_id

end

class Employee \< ActiveRecord::Base  
has\_many :pictures, as: :imageable  
end

class Product \< ActiveRecord::Base  
has\_many :pictures, as: :imageable  
end

Le samedi 27 décembre 2014 17:00:35 UTC+1, Vivek S. a écrit :

---

<div class="post-metadata">

**Author:** ![fmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a698b9/32.png) [@fmh](https://www.ruby-forum.com/u/fmh)\
**Post date:** [December 27, 2014, 5:55pm UTC](https://www.ruby-forum.com/t/has-many-with-model-name-as-foreign-key/233595/4 "2014-12-27T17:55:18Z")

</div>

On 27 December 2014 at 16:19, fmh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thx,
> 
> but with polymorphic associations, i always need to set a foreign key  
> column,
> 
> my need is to set only the column type (model name)…

Explain the problem that you are trying to solve with this unusual  
scheme. It may well be that there is a better way of solving it.

Colin

---

<div class="post-metadata">

**Author:** ![fmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a698b9/32.png) [@fmh](https://www.ruby-forum.com/u/fmh)\
**Post date:** [December 27, 2014, 6:12pm UTC](https://www.ruby-forum.com/t/has-many-with-model-name-as-foreign-key/233595/5 "2014-12-27T18:12:36Z")

</div>

i have many model that user want add or remove attribute dynamically

example :

table / model : product (id, name, price, metadata (serialized column ,  
hash) )  
table /mode : other\_model (id, name, xxx\_attribute, , metadata  
(serialized  
column , hash) )

table / model : my\_custom\_fields (id, name , field\_type , model (column  
contain model name) )

form and fields view are generate dynamically (with partial)

i can do it by :

class product \< AR::Base  
serialize :metadata , Hash

#i save all my custom attributes in metadata column

def get\_all\_custom\_attributes  
My\_custom\_field.find\_by\_model(self.class.name)  
end

end

but i need a properly way if there is one 😉

Le samedi 27 décembre 2014 17:54:13 UTC+1, Colin L. a écrit :
