1
我需要計算在子查詢中其他表中的行,所以我用這個查詢:Rails的子查詢始終返回零值
follows_sql = Follow.where(followable_type: 'Idea').where('follows.followable_id = ideas.id').select('COUNT(followable_id)').to_sql
idea = Idea.select("(#{follows_sql}) AS fcnt").includes(:collaborations).
where(collaborations: { user_id: 4, owner: true })
所以其生產的有效的SQL,但我從構思VAR不能訪問「FCNT的價值。我試圖用不同的方式來做:
idea[0].fcnt # return nil
idea[0]["fcnt"] # return nil
但我只能訪問存在於Idea模型中的字段。 如何訪問我的自定義「FCNT」字段?
是什麼'的想法[0] .attributes'說,清理你的代碼? –
列表中的所有想法的屬性,它存在於數據庫 – MeetJoeBlack
'型號attributes'哈希包含了相應的結果集的每一行的所有牽強領域。顯然這個選擇在某個時候出現了。嗯... –