具有has_many关系和class_name选项的RoR模型;符号vs字符串vs类

时间:2016-11-29 18:25:51

标签: ruby-on-rails activerecord model foreign-keys

对于具有has_many relation和class_name选项的RoR模型,它是最佳实践的类型;符号,字符串或类

例如:

class Customer < ActiveRecord::Base

  # as symbol
  has_many :invoices, class_name: :CustomerInvoices

  # as string
  has_many :invoices, class_name: "CustomerInvoices"

  # as symbol
  has_many :invoices, class_name: CustomerInvoices
end

我测试每个代码变体,它的工作方式相同(显然)。

1 个答案:

答案 0 :(得分:1)

社区驱动的ruby style guide说你should only use snake case for symbols,所以我说你的第一个例子被排除了。

我已经看过其他两种方法都使用了。 Rails style guide对此事保持沉默。我会说选择一个并保持一致使用它。

相关问题