美文网首页
Rails数据库高级查询

Rails数据库高级查询

作者: 风暴英雄 | 来源:发表于2014-04-12 17:36 被阅读0次
使用where方式
>> Aricle.where(:title => 'Advanced Active Record')
=> [#<Article id: 6, title: "Advanced Active Record", ...>]
使用原生SQL语句
>> Article.where("created_at > '23-04-2013' OR body NOT LIKE '%model%'")
=> [#<Article id: 7, title: "One-to-many associations", ...>,
#<Article id: 8, title: "Associations", ...>]


>> Article.where("created_at > '23-04-2013' AND body NOT LIKE '%model%'")
=> []
使用数组条件语义
>> Article.where("published_at < ?", Time.now)
=> [#<Article id: 6, title: "Advanced Active Record", ...>]

>> Article.where("published_at < ?", Time.now).to_sql
=> "SELECT
\"articles\".* FROM
\"articles\"
WHERE
(published_at < '2013-04-02 16:27:51.059277')"

>> Article.where("created_at = ?", Article.last.created_at)
=> [#<Article id: 8, title: "Associations", ...>]

>> Article.where("created_at = ? OR body LIKE ?", Article.last.created_at, 'model')
=> [#<Article id: 8, title: "Associations", ...>]

>> Article.where("title LIKE :search OR body LIKE :search",
{:search => '%association%'})
=> [#<Article id: 7, title: "One-to-many associations", ...>,
#<Article id: 8, title: "Associations", ...>]
使用代理模式
>> User.first.articles.all
=> [#<Article id: 8, title: "Associations", ...>] 

current_user.articles.create(:title => 'Private', :body => ‘Body here..’)
其他一些常用方法
>> Article.all
=> [#<Article id: 6, title: "Advanced Active Record", ...>,
#<Article id: 7, title: "One-to-many associations", ...>,
#<Article id: 8, title: "Associations", ...>]
>> Article.order("title ASC")
=> [#<Article id: 6, title: "Advanced Active Record", ...>,
#<Article id: 8, title: "Associations", ...>,
#<Article id: 7, title: "One-to-many associations", ...>]
>> Article.limit(1)
=> [#<Article id: 6, title: "Advanced Active Record", ...>]
>> Article.order("title DESC").limit(2)
=> [#<Article id: 7, title: "One-to-many associations", ...>,
#<Article id: 8, title: "Associations", ...>]    
>> Article.all
=> [#<Article id: 6, title: "Advanced Active Record", ...>,
#<Article id: 7, title: "One-to-many associations", ...>,
#<Article id: 8, title: "Associations", ...>]
>> Article.order("title ASC")
=> [#<Article id: 6, title: "Advanced Active Record", ...>,
#<Article id: 8, title: "Associations", ...>,
#<Article id: 7, title: "One-to-many associations", ...>]
>> Article.limit(1)
=> [#<Article id: 6, title: "Advanced Active Record", ...>]
>> Article.order("title DESC").limit(2)
=> [#<Article id: 7, title: "One-to-many associations", ...>,
#<Article id: 8, title: "Associations", ...>]
默认作用域

首先我们看看数据库的默认排序方法
>> Category.all
=> [#<Category id: 1, name: "Programming", ...>, #<Category id: 2, name: "Event", ...>,
#<Category id: 3, name: "Travel", ...>, #<Category id: 4, name: "Music", ..>,
#<Category id: 5, name: "TV", ...>]

在category模型中我们添加default_scope方法

class Category < ActiveRecord::Base
    has_and_belongs_to_many :articles 
    default_scope lambda { order('categories.name') }
end

我们在rails命令行中进行测试

>> reload!
Reloading...
>> Category.all
=> [#<Category id: 2, name: "Event", ...>, #<Category id: 4, name: "Music", ...>,
#<Category id: 1, name: "Programming", ...>, #<Category id: 5, name: "TV", ...>,
#<Category id: 3, name: "Travel", ...>]
自定义作用域

首先我们需要在模型中定义方法,如下

class Article < ActiveRecord::Base
    scope :published, lambda { where("articles.published_at IS NOT NULL") }
    scope :draft, lambda { where("articles.published_at IS NULL") }
    scope :recent, lambda { published.where("articles.published_at > ?",
    1.week.ago.to_date)}
    scope :where_title, lambda { |term| where("articles.title LIKE ?", "%#{term}%") }
     
    def long_title
        "#{title} - #{published_at}"
    end
end

我们在命令行中测试

>> Article.published
=> [#<Article id: 6, title: "Advanced Active Record", ...>]
>> Article.draft
=> [#<Article id: 7, title: "One-to-many associations", ...>,
#<Article id: 8, title: "Associations", ...>]
>> Article.recent
=> [#<Article id: 6, title: "Advanced Active Record", ...>]
>> Article.draft.where_title("one")
=> [#<Article id: 7, title: "One-to-many associations", ...>]
>> Article.where_title("Active")
=> [#<Article id: 6, title: "Advanced Active Record", ...>]

相关文章

  • Rails数据库高级查询

    使用where方式 使用原生SQL语句 使用数组条件语义 使用代理模式 其他一些常用方法 默认作用域 首先我们看看...

  • Rails使用model.find_by_sql手写sql查询无

    Rails提供了数据库查询方法以供我们快捷的去查询数据,以至于让我们不需要太懂sql的查询语句就可以通过Rails...

  • Models and Databases 4.search

    文本查询 使用contains icontains 数据库高级比较方式 使用PGSQL 基于文档的查询 全文搜索:...

  • SQL 查询命令

    一 数据库基本操作 二 高级查询 1. 新建表结构 2. 聚合查询 3. 多表查询 4. 连接查询 关于INNER...

  • 数据库高级查询

    查询相关 1.判断某个属性为null时,用is null; 2.判断某个属性为非null时,用 is not nu...

  • 数据库 - 高级查询

    导入SQL文件: window系统 window把文件放在d/E/F盘根目录 关键字: source 格式: so...

  • 2018-07-14

    创建数据库 1、rails db:setup 如果数据库不存在,则创建数据库,并执行种子数据。2、rails db...

  • 法律调研

    一、要有专业的法律数据库。如北大法宝、法律信息数据库; 二·、掌握高级查询技巧。如加“and”、"not"、'or...

  • 数据库知识点

    数据库知识点 数据库相关概念 mysql安装与使用 navicat的使用 SQL语言的查询(重点) 高级(了解) ...

  • rails笔记(3)

    rails切换到mysql数据库 rails自带的是sqllit3这个数据库,不熟系,所以决定切换到mysql。 ...

网友评论

      本文标题:Rails数据库高级查询

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