1、1对1关系的实现 book与user
belongs_to:user
user.rb
has_one :book #体现两者之间的1对1关系
book.rb
def self.up
create_table :books do |t|
t.integer :user_id #存储 user的 id,
t.string :title
t.string :author
t.timestamps
end
end
user.rb
def self.up
create_table :users do |t|
t.string :name
t.timestamps
end
end
例:
u= User.new
u.id= 2
u.save
b=Book.last
u.book= b
此时,b.user_id = 2
2、 单表继承 admin单表继承user
model层
classAdmin < User
def im
p "i am an admin"
end
end
classUser < ActiveRecord::Base
def im
p "i am an user"
end
end
db/migrate层
admin.rb是不需要的
user.rb
create_table :users do |t|
t.string :name
t.string :type #type为保留字,用于单表继承
t.timestamps
end
例:
a = Admin.new
a
#<Admin id: nil, name: nil, type:"Admin", created_at: nil, updated_at: nil>
#a的type字段为"Admin",此条记录仍保存在User表中
3、 1对多关系 book与department
model层
book.rb
belongs_to:department
department.rb
book.rb
def self.up
create_table :books do |t|
t.integer :department_id
t.string :title
t.string :author
t.timestamps
end
end
department.rb
def self.up
create_table :departments do |t|
t.string :name
t.timestamps
end
end
end
例:
d.name=”销售”
d.save
b.title = “zh”
b.save
d.books <<b
此时,b.department_id= d.id
book.rb
has_many :classifies, :through =>:manifests, :source => :classify
has_many :manifests
has_many :books, :through => :manifests,:source => :book
多对多的关系在第三个文件中体现 manifest.rb
belongs_to :book
belongs_to :classify
book.rb
create_table:books do |t|
t.string :title
t.string :author
t.timestamps
end
classify.rb
create_table :classifies do |t|
t.string :kind
t.timestamps
end
manifest.rb #此文件中需要存储有多对多关系对象的id
create_table :manifests do |t|
t.integer :book_id
t.integer :classify_id
end
c= Classify.new
c.kind = "reading"
>>c.save
c= Classify.new
c.kind = "science"
>>c.save
book表为
id title
1 ch
2 en
3 jap
b = Book.first
c =Classify.last
c.books << b #建立book与 Classify之间的关系
manifest表为:
id book_id Classify_id
1 1 2
b= Book.last
b.classifies << c
manifest表为:
id book_id Classify_id
1 1 2
2 3 2
5、多态 book、user、department
model层
belongs_to:borrowed,:polymorphic => true
department.rb
has_many:books,:as => :borrowed, :dependent=> :destroy # dependent的含义是如果book消失,则department所对应的对象消失
def im
p "i am an department"
end
use.rb
has_many:books,:as => :borrowed, :dependent => :destroy
defim
p "i am an user"
end
db/migrate层
book.rb
create_table :books do |t|
t.integer :category_id
t.integer :user_id
t.string :title
t.string :author
t.string :borrowed_type
t.integer :borrowed_id
t.integer :department_id
t.timestamps
end
例:
u= User.first
u.im #"i am an user"
d.im #"i am an department"
u. books = Book.last #此时book表中Book.last 所对应对象的borrowed_type更新为user,borrowed_id更新为u. id
d.books = Book.first #此时book表中Book.first 所对应对象的borrowed_type更新为department,borrowed_id更新为d. id
6、named_scope(用类名来调用)
model层
book.rb
named_scope :name,:conditions => “SQL 语句”, order=>“SQL 语句”, group=>”字段名”
named_scope :unborrowed ,:conditions =>"borrowed_id is null",:order =>"title desc"
named_scope :all_borrowed_type ,:group =>"borrowed_type"
例:
Book.unborrowed #查出 borrowed_id字段为 null的对象,并按 title的降序排列