【问题标题】:Rails array associationRails 数组关联
【发布时间】:2014-04-01 16:57:25
【问题描述】:

我在 Rails 中有 3 个模型之间的关联

class A < ApplicationController
  has_many :b
end


class B < ApplicationController
  has_many :c
  belongs_to :a
end


class C < ApplicationController
  belongs_to :b
end

当我在终端中写入时:

A.first.b.first.c

完美运行

但是当我写的时候

A.first.b.c
A.first.b.all.c

然后rails返回数组[]

如何获得与 A.first 关联的所有 c 模型,但在关联中不使用槽。

【问题讨论】:

    标签: ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.1


    【解决方案1】:

    你可以使用map:

    A.first.b.map(&:c)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-07
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      相关资源
      最近更新 更多