• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby on Rails中的ActiveRecord编程指南

Ruby on Rails中的ActiveRecord编程指南

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了ruby on rails,ruby on rails教程,ruby on rails安装,ruby on rails下载,ruby on rails指南等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com


    避免改动缺省的 ActiveRecord(表的名字、主键,等等),除非你有一个非常好的理由(像是不受你控制的数据库)。
    把宏风格的方法放在类别定义的前面(has_many, validates, 等等)。

    偏好 has_many :through 胜于 has_and_belongs_to_many。 使用 has_many :through 允许在 join 模型有附加的属性及验证

   

 # 使用 has_and_belongs_to_many
  class User < ActiveRecord::Base
   has_and_belongs_to_many :groups
  end

  class Group < ActiveRecord::Base
   has_and_belongs_to_many :users
  end

  # 偏好方式 - using has_many :through
  class User < ActiveRecord::Base
   has_many :memberships
   has_many :groups, through: :memberships
  end

  class Membership < ActiveRecord::Base
   belongs_to :user
   belongs_to :group
  end

  class Group < ActiveRecord::Base
   has_many :memberships
   has_many :users, through: :memberships
  end

</div>

    使用新的 "sexy" validation。

    当一个惯用的验证使用超过一次或验证是某个正则表达映射时,创建一个惯用的 validator 文件。

  # 差
  class Person
   validates :email, format: { with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
  end

  # 好
  class EmailValidator < ActiveModel::EachValidator
   def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || 'is not a valid email') unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
   end
  end

  class Person
   validates :email, email: true
  end

</div>

    所有惯用的验证器应放在一个共享的 gem 。

    自由地使用命名的作用域(scope)。

   

 class User < ActiveRecord::Base
   scope :active, -> { where(active: true) }
   scope :inactive, -> { where(active: false) }

   scope :with_orders, -> { joins(:orders).select('distinct(users.id)') }
  end

</div>

    将命名的作用域包在 lambda 里来惰性地初始化。

 

  # 差劲
  class User < ActiveRecord::Base
   scope :active, where(active: true)
   scope :inactive, where(active: false)

   scope :with_orders, joins(:orders).select('distinct(users.id)')
  end

  # 好
  class User < ActiveRecord::Base
   scope :active, -> { where(active: true) }
   scope :inactive, -> { where(active: false) }

   scope :with_orders, -> { joins(:orders).select('distinct(users.id)') }
  end

</div>

    当一个由 lambda 及参数定义的作用域变得过于复杂时,更好的方式是建一个作为同样用途的类别方法,并返回一个 ActiveRecord::Relation 对象。你也可以这么定义出更精简的作用域。

  class User < ActiveRecord::Base
   def self.with_orders
    joins(:orders).select('distinct(users.id)')
   end
  end

</div>

    注意 update_attribute 方法的行为。它不运行模型验证(不同于 update_attributes )并且可能把模型状态给搞砸。

    使用用户友好的网址。在网址显示具描述性的模型属性,而不只是 id 。
    有不止一种方法可以达成:

        覆写模型的 to_param 方法。这是 Rails 用来给对象建构网址的方法。缺省的实作会以字串形式返回该 id 的记录。它可被另一个具人类可读的属性覆写。

    class Person
     def to_param
      "#{id} #{name}".parameterize
     end
    end

</div>

    为了要转换成对网址友好 (URL-friendly)的数值,字串应当调用 parameterize 。 对象的 id 要放在开头,以便给 ActiveRecord 的 find 方法查找。
    * 使用此 friendly_id gem。它允许藉由某些具描述性的模型属性,而不是用 id 来创建人类可读的网址。

  Ruby
  class Person
  extend FriendlyId
  friendly_id :name, use: :slugged
  end

</div>

    查看 gem 文档获得更多关于使用的信息。

</div>

您可能想查找下面的文章:

  • Rails bundle命令安装mysql gem包出错的解决方法
  • win7安装ruby on rails开发环境
  • 简单对比分析Ruby on Rails 和 Laravel
  • 在 Ubuntu 12.04 Server 上安装部署 Ruby on Rails 应用
  • 快速正确的安装 Ruby, Rails 运行环境
  • Windows下Ruby on Rails开发环境安装配置图文教程
  • Ruby On Rails中如何避免N+1问题
  • 艰难完成 nginx + puma 部署 rails 4的详细记录
  • 浅谈Rails 4 中Strong Parameters机制
  • Rails命令行常用操作命令简明总结

相关文章

  • Ruby语言中的String深入理解
  • 在Ruby中利用Net::SMTP类发送电子邮件的教程
  • Ruby中实现把字符串转换为类的2种方法
  • 快速正确的安装 Ruby, Rails 运行环境
  • 使用rbenv来管理Ruby版本的方法
  • 以MVC的思维方式来理解Ruby on Rails框架的设计结构
  • Rails脚手架使用实例
  • Ruby的基础语法入门学习教程
  • Ruby中使用连续体Continuation实现生成器
  • Ruby 取得指定月日期数的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 收集的多个ruby遍历文件夹代码实例
    • Ruby和元编程之万物皆为对象
    • Rails中遇到错误跳转到统一提示错误页的方法
    • ruby on rails 代码技巧
    • Ruby中的public、private、protected区别小结
    • ruby 对象的初始化 方法
    • Ruby简明教程之方法(Method)介绍
    • ruby线程实现生产者消费者问题示例(队列Queue实现线程同步)
    • 进一步深入Ruby中的类与对象概念
    • 在阿里云 (aliyun) 服务器上搭建Ruby On Rails环境

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有