避免改動缺省的 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
使用新的 "sexy" validation。
當一個慣用的驗證使用超過一次或驗證是某個正則表達映射時,創(chuàng)建一個慣用的 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
所有慣用的驗證器應放在一個共享的 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 里來惰性地初始化。
# 差勁
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
當一個由 lambda 及參數定義的作用域變得過于復雜時,更好的方式是建一個作為同樣用途的類別方法,并返回一個 ActiveRecord::Relation 對象。你也可以這么定義出更精簡的作用域。
class User ActiveRecord::Base
def self.with_orders
joins(:orders).select('distinct(users.id)')
end
end
注意 update_attribute 方法的行為。它不運行模型驗證(不同于 update_attributes )并且可能把模型狀態(tài)給搞砸。
使用用戶友好的網址。在網址顯示具描述性的模型屬性,而不只是 id 。
有不止一種方法可以達成:
覆寫模型的 to_param 方法。這是 Rails 用來給對象建構網址的方法。缺省的實作會以字串形式返回該 id 的記錄。它可被另一個具人類可讀的屬性覆寫。
class Person
def to_param
"#{id} #{name}".parameterize
end
end
為了要轉換成對網址友好 (URL-friendly)的數值,字串應當調用 parameterize 。 對象的 id 要放在開頭,以便給 ActiveRecord 的 find 方法查找。
* 使用此 friendly_id gem。它允許藉由某些具描述性的模型屬性,而不是用 id 來創(chuàng)建人類可讀的網址。
Ruby
class Person
extend FriendlyId
friendly_id :name, use: :slugged
end
查看 gem 文檔獲得更多關于使用的信息。
您可能感興趣的文章:- 關于Ruby on Rails路由配置的一些建議
- 快速安裝Ruby on Rails的簡明指南
- 關于Ruby on Rails視圖編寫的一些建議