Rails 5.0.0.1,ActiveRecord包含select和has_many

时间:2016-12-12 02:44:32

标签: activerecord ruby-on-rails-5

我正在使用这行代码生成我的表:

def index
  @documentation_languages = Documentation::Language
    .includes(:tags)
    .select(:id, :abbreviation, :name, :version)
    .order(:slug)
end

他们会致电:

SELECT "documentation_languages"."id", "documentation_languages"."abbreviation", "documentation_languages"."name", "documentation_languages"."version" FROM "documentation_languages" ORDER BY "documentation_languages"."slug" ASC
SELECT "documentation_language_tags".* FROM "documentation_language_tags" WHERE "documentation_language_tags"."language_id" IN (2, 3, 1, 4, 5, 6, 7, 8, 9, 10, 11, 13, 12, 14, 15, 16, 17)
SELECT "documentation_tags".* FROM "documentation_tags" WHERE "documentation_tags"."id" = 1

我在has_many :tags, through: :language_tags模型中使用Documentation::Language。我可以从包含的documentation_tags.name表格中选择tags这样的任何方式吗?

1 个答案:

答案 0 :(得分:2)

我使用scope的{​​{1}}参数找出来并将代码更改为:

has_many