has_many with model name as foreign key

57 views
Skip to first unread message

fmh

unread,
Dec 26, 2014, 8:17:43 PM12/26/14
to rubyonra...@googlegroups.com
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



Vivek Sampara

unread,
Dec 27, 2014, 11:00:35 AM12/27/14
to rubyonra...@googlegroups.com
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  in rails. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e5498711-d424-42bf-bbed-aebedc3a64d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fmh

unread,
Dec 27, 2014, 11:19:33 AM12/27/14
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Dec 27, 2014, 11:54:13 AM12/27/14
to rubyonra...@googlegroups.com
On 27 December 2014 at 16:19, fmh <aph...@gmail.com> 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

fmh

unread,
Dec 27, 2014, 12:11:31 PM12/27/14
to rubyonra...@googlegroups.com
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 ;)
Reply all
Reply to author
Forward
0 new messages