ruby on rails relationship between tables, multiple fields in one table related to another table -


i have 2 tables: person , batch

a batch has 3 fields relates person table: supervisor, creator , modifier.

all 3 fields store person_id of person table.

i created following relationship between person , batch models.

class batch < activerecord::base     belongs_to :person, :foreign_key => "supervisor_id" end  class person < activerecord::base     has_many :batches, :foreign_key => "supervisor_id" end 

so if 'batch.person.per_name', supervisor's name..but how go on creator , modifier details?

many suggestion provided

it sounds want 3 different relationships:

class batch < activerecord::base   belongs_to :supervisor   belongs_to :creator   belongs_to :modifier end 

then can reference each 1 independently as: batch.supervisor batch.creator batch.modifier

alternatively, create join table between 2 , arbitrarily add , remove linkages:

class batch < activerecord::base   has_many :person_batches   has_many :people, :through => :person_batches, :source => :persons # end  class person < activerecord::base   has_many :batches, :through => :person_batches end  class personbatches < activerecord::base   belongs_to :person   belongs_to :batch    # has column called :person_type can specify   # "supervisor," "creator," "modifier," etc.   # allow batch have multiples of each, if that's   # useful. otherwise, can add validations around this. end 

Comments

Popular posts from this blog

Cursor error with postgresql, pgpool and php -

c# - how to write client side events functions for the combobox items -

ssh client with paramiko (python) -