ruby - Rails belongs_to relationship with multiple possibilities -


i'm trying make relationship model, information, belongs_to either user or client.

i thought putting in information.rb

belongs_to :user belongs_to :client 

and in user.rb , client.rb

has_one :information

but makes information belong_to both user , client.

is there way make can belong either or without leaving 1 of fields blank?

p.s. if needed i'm using rails 4.2, ruby 2.2.1, , devise account authentication.

thanks!

this sounds unusual association it's fit polymorphic association. in case, declare name association

class information < activerecord::base   belongs_to :informational, polymorphic: true #or  class user < activerecord::base   has_many informations, :informational  class client < activerecord::base   has_many informations, :informational 

and need add 2 columns information informational_id, :integer , informational_type, :string

and client , user need integer called informational_id indexed.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -