Question

Im quite new to Ruby on Rails, I've only completed Michael Hart railstutorial.org and now with tutorial as a base of my new project I'm working on simple electronic prescription service. What I feel very confused about is many-to-many relations in RoR and it would be nice to avoid them but I am not sure if thats possible with functionality that I need.

Here is my basic EER with relations table that is going to connect prescription with medicines that it should include.

enter image description here

I would much appreciate any ideas about how to simplify this one, or maybe it's not really that hard to implement?

Était-ce utile?

La solution

Don't avoid the many-to-many association, it's only about working with a join model

You can just use has_many :through

enter image description here

You'd just need to do this:

#app/models/prescription.rb
Class Prescription < ActiveRecord::Base
    has_many :relations
    has_many :medicines, through: :relations
end

#app/models/relation.rb
Class Relation < ActiveRecord::Base
    belongs_to :prescription
    belongs_to :medicine
end

#app/models/medicine.rb
Class Medicine < ActiveRecord::Base
    has_many :relations
    has_many :prescriptions, through: :relations
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top