簡體   English   中英

ruby on rails包括無法為has_many關聯獲取數據

[英]ruby on rails includes not working to get data for has_many association

我創建了下面定義的兩個模型

class TrendGraph < ActiveRecord::Base
  acts_as_paranoid
  has_many :trends
end


class Trend < ActiveRecord::Base
  acts_as_paranoid
end

現在,我正在嘗試提取每個圖形的數​​據以及相關趨勢數據。 我正在使用以下查詢

@trendGraphs =    TrendGraph.where("column=?",@someValue).all.includes(:trends)

但是我在@trendGraphs中獲得的只是趨勢圖表數據,而不是趨勢表中的相關數據。 我需要的是下面的東西

TrendGraphData1:{key1:"value1",key2:"value2",trends:[{trendk1:"trendvalue1"},{trendk2:"trendvalue2"}]}

我能夠使用laravel中的with子句實現這一點,而只是從laravel轉到ROR。 能否請我告訴我我在做錯什么以及如何實現?

提前致謝。

嘗試添加

belongs_to :trend_graph

Trend類中

您可以這樣做:

@trend_graphs = TrendGraph.where(column: @someValue).all.includes(:trends).as_json(include: :trends)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM