質問

I have an association in rails between 3 models

class A < ApplicationController
  has_many :b
end


class B < ApplicationController
  has_many :c
  belongs_to :a
end


class C < ApplicationController
  belongs_to :b
end

when I write in the terminal:

A.first.b.first.c

works perfectly

but when I write

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

then rails return array []

how can I get all the c models associated to A.first but withouth use trough in the associations.

役に立ちましたか?

解決

You can use map:

A.first.b.map(&:c)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top