gpt4 book ai didi

ruby-on-rails - 递归 :include in Rails ActiveRecord

转载 作者:行者123 更新时间:2023-12-04 22:53:19 27 4
gpt4 key购买 nike

说我有这些模型

class Project < ActiveRecord::Base
has_many :comments
end

class Comment < ActiveRecord::Base
belongs_to :project
belongs_to :user
end

class User < ActiveRecord::Base
has_many :comments
end

这样我才能做到
p = Project.find(1, :include => :comments)
p.comments.collect(&:user).collect(&:name) # this executes select for each user

我怎么说我还想包括评论的用户?

最佳答案

我相信 :include => {:comments => :user}应该管用。

关于ruby-on-rails - 递归 :include in Rails ActiveRecord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1427823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com