这种情况下是否存在一个助手?Rails递归搜索助手
您有Survey
,其中has_many
问题,其中has_many
答案。
s = Survey.first
s.answers # => Returns the answers of all the survey questions
我不想在这里重新创建轮子。
这种情况下是否存在一个助手?Rails递归搜索助手
您有Survey
,其中has_many
问题,其中has_many
答案。
s = Survey.first
s.answers # => Returns the answers of all the survey questions
我不想在这里重新创建轮子。
它是建立在:
class Survey < ActiveRecord::Base
has_many :questions
has_many :answers, :through => :questions
# ...
end
就是这样。现在你可以拨打survey.answers
,它会得到所有的答案。
阅读上的:through
选项(和它的局限性)here
一个Rails助手什么情况? –