You need three tables to represent a many-to-many association.
class Owner < ActiveRecord::Base
has_and_belongs_to_many :datas
end
class Data < ActiveRecord::Base
has_and_belongs_to_many :owners
end
Table owners:
id: integer
name: string
Table datas:
id: integer
value: integer
Table datas_owners:
data_id: integer
owner_id: integer
This is shown using the more basic has_and_belongs_to_many (HABTM)
associations. However, in some cases it’s necessary to store
additional information in the join table managing your many-to-many
association. In which case your can make the join table into model
class and use has_many :through.
Having owner1_id, owner2_id is in violation of the “First Normal Form
(1NF)” relational database rule.
the owner1_id corresponds to the student associated with the entry.
the owner2_id corresponds to the teacher associated with the entry.
i just used the owner1 and 2 for sake of simplicity.
there is a table of students and teachers with a user level to decide
what they are thats used for basic information and authentication. it
seemed more logical to treat it that way… please correct me if i’m
wrong… i’m a little tarded when it comes to database design.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.