在阅读的过程中有不论什么问题,欢迎一起交流
QQ:1494713801
一、怎样定义关联
两个model之间经常会存在关联关系,为了解决这些关联引起的复杂操作问题,能够在model定义时定义其关联关系。如:实体customers和orders定义例如以下:
class
has_many :orders , dependent: :destroy end class
belongs_to :customer end |
belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many
1、belongs_to(与has_many相应)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="368" height="145">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="335" height="145">
如:病i人预约医生的样例,每一个预约都相应一个医生和一个病人。可是通过预约医生和病人会有多对多的关系。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="324" height="148">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="339" height="142">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="332" height="101">
:as
:autosave
:class_name
:dependent
:foreign_key
:inverse_of
:primary_key
:source
:source_type
:through
:validate
1、as:指明为多态关联