新建Model
建立一個(gè)新的Model
rails g model category
請(qǐng)注意產(chǎn)生的xxxxx_create_categories.rb文件憨愉,這個(gè)文件記錄了categories表的定義步淹,我們可以給表增加幾個(gè)列
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
我們同時(shí)給events加上一個(gè)外鍵瘪菌,讓這兩張表之間可以關(guān)聯(lián)起來。
新建數(shù)據(jù)表:
rake db:migrate
Model關(guān)聯(lián)
一對(duì)多關(guān)聯(lián)one-to-many
在上圖中一個(gè)Event可以有多個(gè)Attendee线召,我們新建attendee Model
rails g model attendee name:string event_id:integer
生成數(shù)據(jù)表attendees
rake db:migrate
分別編輯event.rb和attendee.rb建立關(guān)聯(lián)關(guān)系
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對(duì)象并關(guān)聯(lián)到Event上:
e = Event.first
a = Attendee.new(:name => 'ihower', :event => e)
# a = Attendee.new(:name => 'ihower', :event_id => e.id)
a.save
e.attendees #這是個(gè)數(shù)組
Attendee.first.event
范例二、從Event對(duì)象中建立一個(gè)Attendee
e = Event.first
a = e.attendees.build(:name => 'ihower')
a.save
范例三、從Event對(duì)象中建立一個(gè)Attendee亏较,并直接存到數(shù)據(jù)庫
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 #一個(gè)個(gè)刪除e的attendees雪情,并觸發(fā)attendee的destroy回調(diào)
e.attendees.delete_all #一次性刪除e的attendees癣朗,不會(huì)觸發(fā)attendee的destroy回調(diào)
一對(duì)一關(guān)聯(lián) one-to-one
假設(shè)每一個(gè)event都有一個(gè)location,增加一個(gè)Location Model旺罢,其中event_id就是外鍵
rails g model location name:string event_id:integer
然后執(zhí)行rake db:migrate產(chǎn)生數(shù)據(jù)表.
分別編輯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對(duì)象并關(guān)聯(lián)到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對(duì)象建立一個(gè)Location
e = Event.first
l = e.build_location(:name=>'Hsinchu')
l.save
范例三扁达、直接從Event對(duì)象中建立一個(gè)Location
e = Event.first
l = e.create_location(:name=>'Hsinchu')
多對(duì)多關(guān)聯(lián)many-to-many
一個(gè)Event有多個(gè)Group,一個(gè)Group有多個(gè)Event蠢熄。我們可以用一個(gè)額外的Model來建立關(guān)聯(lián)跪解。
我們可以建立如下的model
rails g model group name:string
rails g model event_groupship event_id:integer group_id:integer
執(zhí)行rake db:migrate產(chǎn)生這連個(gè)數(shù)據(jù)表。
分別編輯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
范例叉讥、建立雙向關(guān)聯(lián)
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建立多對(duì)多關(guān)聯(lián)
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
執(zhí)行rake db:migrate生成數(shù)據(jù)庫表
編輯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
關(guān)聯(lián)參數(shù)
order指定順序
class Event < ActiveRecord::Base
has_many :attendees, ->{ order("id DESC") }
end
串聯(lián)where條件
class Event < ActiveRecord::Base
has_many :attendees, ->{ where(["created_at > ?", Time.now - 7.day]).order("id DESC") }
end
刪除依賴數(shù)據(jù)
class Event < ActiveRecord::Base
has_one :location, :dependent => :destroy
has_many :attendees, :dependent => :destroy
end