Ruby on Rails中的ActiveRecord編程指南
避免改動(dòng)缺省的 ActiveRecord(表的名字、主鍵,等等),除非你有一個(gè)非常好的理由(像是不受你控制的數(shù)據(jù)庫(kù))。
把宏風(fēng)格的方法放在類(lèi)別定義的前面(has_many, validates, 等等)。
偏好 has_many :through 勝于 has_and_belongs_to_many。 使用 has_many :through 允許在 join 模型有附加的屬性及驗(yàn)證
# 使用 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
使用新的 。
當(dāng)一個(gè)慣用的驗(yàn)證使用超過(guò)一次或驗(yàn)證是某個(gè)正則表達(dá)映射時(shí),創(chuàng)建一個(gè)慣用的 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
所有慣用的驗(yàn)證器應(yīng)放在一個(gè)共享的 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
將命名的作用域包在 lambda 里來(lái)惰性地初始化。
# 差勁 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
當(dāng)一個(gè)由 lambda 及參數(shù)定義的作用域變得過(guò)于復(fù)雜時(shí),更好的方式是建一個(gè)作為同樣用途的類(lèi)別方法,并返回一個(gè) ActiveRecord::Relation 對(duì)象。你也可以這么定義出更精簡(jiǎn)的作用域。
class User < ActiveRecord::Base def self.with_orders joins(:orders).select('distinct(users.id)') end end
注意 update_attribute 方法的行為。它不運(yùn)行模型驗(yàn)證(不同于 update_attributes )并且可能把模型狀態(tài)給搞砸。
使用用戶(hù)友好的網(wǎng)址。在網(wǎng)址顯示具描述性的模型屬性,而不只是 id 。
有不止一種方法可以達(dá)成:
覆寫(xiě)模型的 to_param 方法。這是 Rails 用來(lái)給對(duì)象建構(gòu)網(wǎng)址的方法。缺省的實(shí)作會(huì)以字串形式返回該 id 的記錄。它可被另一個(gè)具人類(lèi)可讀的屬性覆寫(xiě)。
class Person def to_param "#{id} #{name}".parameterize end end
為了要轉(zhuǎn)換成對(duì)網(wǎng)址友好 (URL-friendly)的數(shù)值,字串應(yīng)當(dāng)調(diào)用 parameterize 。 對(duì)象的 id 要放在開(kāi)頭,以便給 ActiveRecord 的 find 方法查找。
* 使用此 friendly_id gem。它允許藉由某些具描述性的模型屬性,而不是用 id 來(lái)創(chuàng)建人類(lèi)可讀的網(wǎng)址。
Ruby class Person extend FriendlyId friendly_id :name, use: :slugged end
查看 gem 文檔獲得更多關(guān)于使用的信息。
相關(guān)文章
Ruby設(shè)計(jì)模式編程中對(duì)外觀模式的應(yīng)用實(shí)例分析
這篇文章主要介紹了Ruby設(shè)計(jì)模式編程中對(duì)外觀模式的應(yīng)用實(shí)例分析,外觀模式在Ruby on Rails開(kāi)發(fā)項(xiàng)目中也經(jīng)常被用到,需要的朋友可以參考下2016-03-03ruby 學(xué)習(xí)筆記(1) 初識(shí)語(yǔ)法
雖然ruby/ruby on rails從2007年就一直獲獎(jiǎng)無(wú)數(shù),但身為一個(gè)中國(guó)人,一直對(duì)小日本創(chuàng)造的東西不怎么感興趣,想想其實(shí)也沒(méi)必要,技術(shù)本身是無(wú)國(guó)界的,日本其實(shí)也有值得學(xué)習(xí)的地方(扯遠(yuǎn)了,呵)2010-02-02Ruby創(chuàng)建“關(guān)鍵字”同名方法別名的方法
這篇文章主要介紹了Ruby創(chuàng)建“關(guān)鍵字”同名方法別名的方法,本文提示的是一個(gè)小技巧,特殊場(chǎng)景時(shí)可能會(huì)用到,需要的朋友可以參考下2015-01-01用實(shí)際代碼演示Ruby的容易被誤解的6個(gè)特性
這篇文章主要介紹了用實(shí)際代碼演示Ruby的容易被誤解的6個(gè)特性,采集自IBM官方網(wǎng)站的開(kāi)發(fā)者文檔,需要的朋友可以參考下2015-04-04Ruby on Rails在Ping ++ 平臺(tái)實(shí)現(xiàn)支付
本文給大家分享的是使用Ruby on Rails在Ping ++ 平臺(tái)實(shí)現(xiàn)支付功能的代碼,非常的實(shí)用,有需要的小伙伴可以參考下。2016-02-02Ruby面向?qū)ο缶幊讨蓄?lèi)與方法的基礎(chǔ)學(xué)習(xí)
方法可以理解為類(lèi)中的函數(shù),一定程度上可以受到類(lèi)作用域的制約,其他地方和傳統(tǒng)意義上的函數(shù)無(wú)太大區(qū)別,這里我們就一起來(lái)進(jìn)行Ruby面向?qū)ο缶幊讨蓄?lèi)與方法的基礎(chǔ)學(xué)習(xí)2016-05-05