美文网首页
rails模型关联

rails模型关联

作者: 李傲娢 | 来源:发表于2018-03-25 19:53 被阅读21次

rails的orm很完善,可以非常方便的建立模型之间的关联.通过一些简单的代码实现一些常见的关联操作.

关联基础操作

最常见的关联方式就是一对多和一对一.实现的时候有两个基础的方法,has_many和belongs_to.根据字面上的意思理解,has_many表示拥有很多,belongs_to表示属于.

通过一个书籍和书籍分类的例子做一个总结

# 在app/models目录中生成相关的文件
rails g model book
rails g model type

修改数据库合并文件db/migrate,添加相关的字段

# book
class CreateBooks < ActiveRecord::Migration[5.1]
  def change
    create_table :books do |t|
      t.string :title
      t.string :description
      t.text :content
      t.integer :type_id # 关联types表
      t.timestamps
    end
  end
end
# type
class CreateTypes < ActiveRecord::Migration[5.1]
  def change
    create_table :types do |t|
      t.string :name
      t.timestamps
    end
  end
end

修改书籍模型app/models/book.rb

class Book < ApplicationRecord
  belongs_to :type
end

修改书籍分类app/models/type.rb

class Type < ApplicationRecord
  has_many :books
end

创建一个实例

book = Book.create(title: '三国演义', description: '古典名著,中国四大名著之一') # 创建一个book实例
book.type.create(name: '古典名著') # 创建一个分类信息

# 通过type_id指定分类信息
shuihuzhuan = Book.create(title: '水浒传', description: '经典四大名著,105个男人和三个女人的故事',type_id: 1)

添加belongs_to之后,每一个模型的实例都将获得以下方法

# association
#  如果关联的对象存在就返回,否则为nil

# association=(associate)
#  设置关联对象

# build_association(attributes = {})
#   设置一个新的对象,根据传递的参数.临时存储,不保存数据库

# create_association(attributes = {})
#   设置新的关联对象,验证通过后存数据库

# create_association!(attributes = {})
#   设置新的关联对象,验证通过后存数据库.如果记录无效,会抛出异常

添加has_many之后实例获得以下方法

# collection
# collection<<(object, &#8230;&#8203;)
# collection.delete(object, &#8230;&#8203;)
# collection.destroy(object, &#8230;&#8203;)
# collection=(objects)
# collection_singular_ids
# collection_singular_ids=(ids)
# collection.clear
# collection.empty?
# collection.size
# collection.find(&#8230;&#8203;)
# collection.where(&#8230;&#8203;)
# collection.exists?(&#8230;&#8203;)
# collection.build(attributes = {}, &#8230;&#8203;)
# collection.create(attributes = {})
# collection.create!(attributes = {})
# 相关的方法都是一些集合的操作,具体的解释可以参考官网文档

这些基础操作可以查阅官网

多态关联

在实际开发中经常会遇到一个模型属于多个其他模型的情况.如:附件资源模型(存贮照片素材),可以属于用户和产品两个模型.用户有头像,产品有图片都可以存在资源表中,为了区分用户表和产品表的关联内容可以使用多态关联进行操作.

# 创建model
rails g model priture
rails g model user
rails g model product

修改数据库生成文件 db/migrate/xxx

# 创建用户表
class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.string :name
      t.timestamps
    end
  end
end

# 创建产品表
class CreateProducts < ActiveRecord::Migration[5.1]
  def change
    create_table :products do |t|
      t.string :name
      t.timestamps
    end
  end
end

# 创建图片资源表
class CreatePictures < ActiveRecord::Migration[5.1]
  def change
    create_table :pictures do |t|
      t.string :path
      t.string :name
      t.integer :imageable_id
      t.string :imageable_type
      t.timestamps
    end
    add_index :pictures, [:imageable_type, :imageable_id]
  end
end
class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true
end
 
class User < ApplicationRecord
  has_many :pictures, as: :imageable
end
 
class Product < ApplicationRecord
  has_many :pictures, as: :imageable
end

在belongs_to中通过设置polymorphic指定使用多态关联.在User实例上可以使用@user.pictures获取照片集合,在产品实例上使用@product.pictures获取.

当在执行数据创建方法之后,或分别在数据库中创建不同表关联的模型,根据imageable_type字段进行区分.字段存储的是关联的模型名字(User,Product)

可以测试刚才的数据是否可用

u = User.create(name: '小明') # 创建user
u.pictures.create(name: '头像', path: '/uploads/xm.jpg') # 创建picture

# 创建单个附件
p = Product.create(name: '华为mate10')
# 创建多个附件
p.pictures.create([
                    { name: '主图', path: '/uploads/hw_main.png' },
                    { name: '轮播1', path: '/uploads/hw_slider.png' },
                    { name: '轮播', path: '/uploads/hw_slider2.png' },
                ])

相关文章

  • rails模型关联

    rails的orm很完善,可以非常方便的建立模型之间的关联.通过一些简单的代码实现一些常见的关联操作. 关联基础操...

  • [Rails] Active Record Associatio

    资料来源:Rails Guide Guide: -在模型之间构建关联-理解不同种类的关联-使用关联提供的方法 1....

  • 《Rails-Guides》Reading notes five

    Rails 支持六种关联 belongs_to关联 belongs_to关联创建两个模型之间一对一的关系,声明所...

  • Active Record关联

    在开发中常常会涉及到多个模型进行关联的操作.Rails支持六种关联: 为了后续的内容分析,事先创建以下模型 bel...

  • Rails的模型自关联

    关于Rails的模型自关联有一个非常有意思的题目,大概是这样的: 问如何定义Person模型来满足以上需求? 题目...

  • Rails joins & includes &

    又是一个起源于群友(154112964 rails群 欢迎使用rails的所有人)讨论的玩意 先放一些模型&关联在...

  • rails模型关联(二)-自联结

    在实际开发的时候会遇到多级分类的需求.如:商品分类.3c分类下可以包含手机、数码、mp3等二级分类,衣服分类下可以...

  • laravel和thinkphp5模型关联

    遵循1221原则 即 本模型 关联模型,关联模型的关联字段 本模型字段 使用模型 where)->find();d...

  • 模型查询之关联模型与嵌套模型

    模型关联 一对多:hasMany(‘关联模型的模型名’,’关联模型的外键’,’当前模型的主键’) 通过联试方法wi...

  • Rails 的多态关联

    首先很久没有写过 markdown, 真是不进则退,还好 google 了下,要感谢这篇文章 献给写作者的 Mar...

网友评论

      本文标题:rails模型关联

      本文链接:https://www.haomeiwen.com/subject/eejkcftx.html