1.在我们的模型中添加下面方法
class Article < ActiveRecord::Base validates_presence_of :title validates_presence_of :body def long_title "#{title} - #{published_at}" end end
我们就使用上面的方法来操作数据库了
#在命令行中操作模型 >> Article.create :title => 'Advanced Active Record', :published_at => Date.today, :body => 'Models need to relate to each other. In the real world, ...' => #<Article id: 6, title: "Advanced Active Record", ...> >> Article.last.long_title => "Advanced Active Record - 2013-04-22 04:00:00 UTC"
2.数据库之间的关系
①一对一关系
首先:我们创建两张表
rails generate model User email:string password:string
rails generate model Profile user_id:integer name:string birthday:date bio:text color:string twitter:string
我们分别在两个模型中创建对应的方法
#User模型 class User < ActiveRecord::Base has_one :profile end #Profile模型 class Profile < ActiveRecord::Base belongs_to :user end
我们接下来可以在rails命令行中做测试了
>> user = User.create(:email => '[email protected]', :password => 'secret') => #<User id: 1, email: "[email protected]", password: "secret", created_at: "2013-04-02 15:10:07", updated_at: "2013-04-02 15:10:07"> >> profile = Profile.create(:name => 'John Doe', :bio => 'Ruby developer trying to learn Rails') => #<Profile id: 1, user_id: nil, name: "John Doe", birthday: nil, bio: "Ruby developer trying to learn Rails", color: nil, twitter: nil, created_at: "2013-04-02 15:10:55", updated_at: "2013-04-02 15:10:55"> >> user.profile => nil >> user.profile = profile => #<Profile id: 1, user_id: 1, name: "John Doe", birthday: nil, bio: "Ruby developer trying to learn Rails", color: nil, twitter: nil, created_at: "2013-04-02 15:10:55", updated_at: "2013-04-02 15:10:55"> >> user.profile => #<Profile id: 1, user_id: 1, name: "John Doe", birthday: nil, bio: "Ruby developer trying to learn Rails", color: nil, twitter: nil, created_at: "2013-04-02 15:10:55", updated_at: "2013-04-02 15:10:55"> >> user.create_profile :name => 'Jane Doe', :color => 'pink' => #<Profile id: 2, user_id: 1, name: "Jane Doe", birthday: nil, bio: nil, color: "pink", twitter: nil, created_at: "2013-04-02 15:18:57", updated_at: "2013-04-02 15:18:57">
一对一模型中常用方法总结如下:
user.profile 返回user对应的profile对象
user.profile=(profile)对user的profile赋值
user.profile.nil? 返回user的profile是否为空,为空返回真
user.build_profile(attributes={}) 返回一条新的user的profile对象,但是不会保存到数据库,需要使用user.profile.save来保存
user.create_profile(attributes={})返回user的profile对象,直接保存到数据库中
②一对多关系
我们使用user表对应article表,每个用户都有很多的文章,但是每篇文章都只对应一个用户
rails generate migration add_user_id_to_articles user_id:integer
#文章表模型 class Article < ActiveRecord::Base validates_presence_of :title validates_presence_of :body belongs_to :user#每篇文章指定属于用户 def long_title "#{title} - #{published_at}" end end #用户表 class User < ActiveRecord::Base has_one :profile has_many :articles end
我们在rails命令 行中使用示例
>> user = User.first => #<User id: 1, email: "[email protected]", password: "secret", created_at: "2013-04-02 15:10:07", updated_at: "2013-04-02 15:10:07"> >> user.articles => [] >> user.articles << Article.first => [#<Article id: 6, ..., user_id: 1>] >> user.articles.size => 1 >> user.articles => [#<Article id: 6, ..., user_id: 1>] >> Article.first.user_id => 1 >> Article.first.user => #<User id: 1, email: "[email protected]", password: "secret", created_at: "2013-04-02 15:10:07", updated_at: "2013-04-02 15:10:07">
一对多常用方法
user.articles 返回用户模型中所有文章对象
user.articles=(articles) 替换用户模型的所有文章对象,用articles来代替
user.articles << article 添加article对象到user的文章对象中
user.articles.delete(articles) 删除文章模型中的一篇或者多篇文章
user.articles.empty? 判断用户实例的文章是否是空的
user.articles.size 返回用户示例的文章数量
user.article_ids 返回用户示例的文章id,以数组形式返回
user.articles.clear 清空用户的文章
user.articles.find 传入文章的id,返回该文章
user.articles.build(attributes={}) 创建一个新的用户文章对象,不会保存,需要使用user.articles.last.save来保存
user.articles.create(attributes={}) 直接创建文章对象,并且保存到数据库中。
③关系型数据库扩展
i.定义默认排序
#指定单个的排序 class User < ActiveRecord::Base has_one :profile has_many :articles, -> { order('published_at DESC') } end #指定多个字段排序 class User < ActiveRecord::Base has_one :profile has_many :articles, -> { order('published_at DESC, title ASC')} end
ii.特殊依赖
当我们删除一个用户时,该用户的所有文章都会被删除
class User < ActiveRecord::Base has_one :profile has_many :articles, -> { order('published_at DESC, title ASC')}, :dependent => :destroy end
当然我们也可以不删除文章,可以设置被删除用户的所有文章的user_id为空
class User < ActiveRecord::Base has_one :profile has_many :articles, -> { order('published_at DESC, title ASC')}, :dependent => :nullify end
④多对多关系
我们假定一篇文章有很多种分类,一个分类也有很多文章,我们使用rails generate model Category name:string和rails generate migration create_articles_categories分别创建分类表和中间表,中间表的定义如下
#The db/migrate/20130407002156_create_articles_categories.rb: File class CreateArticlesCategories < ActiveRecord::Migration def change create_table :articles_categories, :id => false do |t| t.references :article t.references :category end end def self.down drop_table :articles_categories end end
references方法和integer是一样的效果,也就是说我们可以使用下面的方式来创建add_column :articles_categories, :article_id, :integer,add_column :articles_categories, :category_id, :integer
下面我们分别为Article模型和Category模型添加has_and_belongs_to_many方法
class Article < ActiveRecord::Base has_and_belongs_to_many :categories end class Category < ActiveRecord::Base has_and_belongs_to_many :articles end
下面我们在rails命令行中测试,
>> article = Article.last => #<Article id: 8, title: "Associations", ...> >> category = Category.find_by_name('Programming') => #<Category id: 1, name: "Programming", ..> >> article.categories << category => [#<Category id: 1, name: "Programming", ..>] >> article.categories.any? => true >> article.categories.size => 1 >> category.articles.empty? => false >> category.articles.size => 1 >> category.articles.first.title => "Associations"
多对多关系需要注意,我们可以为每个字段添加索引,并且可以通过数据库来保证每个字段的唯一性
add_index :articles_categories, :article_id add_index :articles_categories, :category_id add_index :articles_categories, [:article_id, :category_id], unique: true
观澜男科医院http://wwww.no1nk.com