写入包括rails4中的嵌套查询

时间:2015-04-27 04:44:37

标签: ruby-on-rails ruby-on-rails-4 join activerecord include

McEntity.joins(:member_credit_scores).includes(member_credit_ent_profile: :entity).where('sf.accountx.namex like ?', "% Time%").first

以上是我的疑问。我期待的是

   mc_entity = {
    entity_sf_account_id: '0014PRD000fkeejAAA', 
    license_date: Date.today, portfolio_type_id: @portfolio_type.id,
    technology_description: "some technology_description", is_rejected: false, credit_value_rating_type_id: @credit_rating_type.id,
    member_credit_scores: {
      "0" => {
        avoided_expense: 151515, likelihood_of_assertion: 15, avoided_expense_notes: "some expense notes",
        likelihood_notes: "some likelihood notes", is_approved: true
      },
      "1" => {
        avoided_expense: 303030, likelihood_of_assertion: 30, avoided_expense_notes: "some random expense notes",
        likelihood_notes: "some random likelihood notes", is_approved: false
      }
    }
  }

但我得到的是没有任何关系数据的McEntity。

   mc_entity = {
    entity_sf_account_id: '0014PRD000fkeejAAA', 
    license_date: Date.today, portfolio_type_id: @portfolio_type.id,
    technology_description: "some technology_description", is_rejected: false, credit_value_rating_type_id: @credit_rating_type.id,

  }

1 个答案:

答案 0 :(得分:1)

实际上正在拉取数据,如果你调用#member_credit_scores方法它将返回数据而不进行查询,如果你想要它在json中,那么你可以将它作为参数传递

entity.to_json(methods: member_credit_scores)

这将调用该方法并将其添加到json对象。