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