新建Model
建立一个新的Model
rails g model category
请注意产生的xxxxx_create_categories.rb文件,这个文件记录了categories表的定义,我们可以给表增加几个列
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name
t.integer :position
t.timestamps
end
add_column :events, :category_id, :integer
add_index :events, :category_id
end
end
我们同时给events加上一个外键,让这两张表之间可以关联起来。
新建数据表:
rake db:migrate
Model关联
一对多关联one-to-many
在上图中一个Event可以有多个Attendee,我们新建attendee Model
rails g model attendee name:string event_id:integer
生成数据表attendees
rake db:migrate
分别编辑event.rb和attendee.rb建立关联关系
class Event < ActiveRecord::Base
has_many :attendees, :class_name=>"Attendee", :foreign_key=>'event_id'
end
class Attendee < ActiveRecord::Base
belongs_to :event, :class_name=>'Event', :foreign_key=>'event_id'
end
范例一、建立Attendee对象并关联到Event上:
e = Event.first
a = Attendee.new(:name => 'ihower', :event => e)
# a = Attendee.new(:name => 'ihower', :event_id => e.id)
a.save
e.attendees #这是个数组
Attendee.first.event
范例二、从Event对象中建立一个Attendee
e = Event.first
a = e.attendees.build(:name => 'ihower')
a.save
范例三、从Event对象中建立一个Attendee,并直接存到数据库
e = Event.first
a = e.attendees.create(:name => 'ihower')
范例四、先建立Attendee再放入Event中
e = Event.first
a = Attendee.create(:name => 'ihower')
e.attendees << a
范例五、删除
e = Event.first
e.attendees.destroy_all #一个个删除e的attendees,并触发attendee的destroy回调
e.attendees.delete_all #一次性删除e的attendees,不会触发attendee的destroy回调
一对一关联 one-to-one
假设每一个event都有一个location,增加一个Location Model,其中event_id就是外键
rails g model location name:string event_id:integer
然后执行rake db:migrate产生数据表.
分别编辑event.rb和location.rb
class Event < ActiveRecord::Base
has_one :location, :class_name=>'Location', :foreign_key=>'event_id'
end
class Location < ActiveRecord::Base
belongs_to :event, :class_name=>'Event', :foreign_key=>'event_id'
end
范例一、建立Location对象并关联到Event
e = Event.first
l = Location.new(:name=>'Hsinchu', :event=>e)
#l = Location.new(:name=>'Hsinchu', :event_id=>e.id)
l.save
e.location
l.event
范例二、从Event对象建立一个Location
e = Event.first
l = e.build_location(:name=>'Hsinchu')
l.save
范例三、直接从Event对象中建立一个Location
e = Event.first
l = e.create_location(:name=>'Hsinchu')
多对多关联many-to-many
一个Event有多个Group,一个Group有多个Event。我们可以用一个额外的Model来建立关联。
我们可以建立如下的model
rails g model group name:string
rails g model event_groupship event_id:integer group_id:integer
执行rake db:migrate产生这连个数据表。
分别编辑event.rb、group.rb和event_groupship.rb文件
class Event < ActiveRecord::Base
has_many :event_groupships
has_many :groups, :through => :event_groupships
end
class EventGroupship < ActiveRecord::Base
belongs_to :event
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :event_groupships
has_many :events, :through => :event_groupships
end
范例、建立双向关联
g = Group.create(:name=>'ruby tw')
e1 = Event.first
e2 = Event.create(:name=>'ruby tuesday')
EventGroupship.create(:event=>e1, :group=>g)
EventGroupship.create( :event => e2, :group => g )
g.events
e1.groups
e2.groups
不通过第三方Model建立多对多关联
rails g model group name:string
rails g migration create_table_event_groupship
编辑xxxx_ create_table_event_groupship.rb文件
class CreateTrainTeacher < ActiveRecord::Migration
def up
create_table :event_groupships do |t|
t.integer :event_id
t.integer :group_id
end
end
def down
drop_table :event_groupships
end
end
执行rake db:migrate生成数据库表
编辑event.rb和group.rb文件
class Event < ActiveRecord::Base
has_and_belongs_to_many :group, :class_name=>'Group', :foreign_key=>'event_id',
:association_foreign_key=>'group_id', :join_table=>'event_grouphips'
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :event, :class_name=>'Event', :foreign_key=>'group_id',
:association_foreign_key=>'event_id', :join_table=>'event_grouphips'
end
关联参数
order指定顺序
class Event < ActiveRecord::Base
has_many :attendees, ->{ order("id DESC") }
end
串联where条件
class Event < ActiveRecord::Base
has_many :attendees, ->{ where(["created_at > ?", Time.now - 7.day]).order("id DESC") }
end
删除依赖数据
class Event < ActiveRecord::Base
has_one :location, :dependent => :destroy
has_many :attendees, :dependent => :destroy
end