gpt4 book ai didi

ruby-on-rails - rails : Query parents with grandchildren (associations)

转载 作者:数据小太阳 更新时间:2023-10-29 08:07:15 25 4
gpt4 key购买 nike

class User < ActiveRecord::Base
has_many :posts
end

class Question < ActiveRecord::Base
belongs_to :user
has_many :answers
end

class Answer < ActiveRecord::Base
belongs_to :question
end

使用 Rails 我可以:

1.返回所有有问题用户?例如。

@users_with_questions = User.joins(:question).where.not(question: { id: nil })

2.返回所有用户answers?

最佳答案

1.返回所有有问题的用户?

@users_with_questions = User.includes(:question).where.not(:question => {id: nil})

2.返回所有用户的答案?

@users_with_answers = User.includes(question: :answer).where.not(:answer => {id: nil})

关于ruby-on-rails - rails : Query parents with grandchildren (associations),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39383519/

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