如何左加入细节?
How to Left join on detail?
我有模型 Customer、CustomerQuestion 和 CustomerAnswer。
Customer
has_many CustomerQuestion
Customer
has_many CustomerAnswer
CustomerQuestion
has_one CustomerAnswer
当我调用 Customer 时,我可以获得详细的 CustomerAnswer,但我想按 CustomerQuestion 中的字段进行排序。我将如何离开加入 CustomerQuestion 到 CustomerAnswer 以供我订购?
我的示例代码,但我不知道下一步该做什么;
@customer = Customer.find(params[:id])
@customer.customer_answers.join("LEFT JOIN customer_questions cq ON cq.id = customer_answers.customer_question_id ").order("ISNULL(cq.order_by), customer_answers.id")
@customer = Customer.find(params[:id])
@customer_answers = @customer.customer_answers.includes(:customer_question).order(id: :desc)
或者您可以只粘贴第 2 行的代码以共享每个迭代器。
我有模型 Customer、CustomerQuestion 和 CustomerAnswer。
Customer
has_many CustomerQuestion
Customer
has_many CustomerAnswer
CustomerQuestion
has_one CustomerAnswer
当我调用 Customer 时,我可以获得详细的 CustomerAnswer,但我想按 CustomerQuestion 中的字段进行排序。我将如何离开加入 CustomerQuestion 到 CustomerAnswer 以供我订购?
我的示例代码,但我不知道下一步该做什么;
@customer = Customer.find(params[:id])
@customer.customer_answers.join("LEFT JOIN customer_questions cq ON cq.id = customer_answers.customer_question_id ").order("ISNULL(cq.order_by), customer_answers.id")
@customer = Customer.find(params[:id])
@customer_answers = @customer.customer_answers.includes(:customer_question).order(id: :desc)
或者您可以只粘贴第 2 行的代码以共享每个迭代器。