如何将一个查询结果映射到rails上的ruby中的另一个查询结果

时间:2016-05-31 06:34:21

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

您好我是Ruby on Rails开发的新手。我有两个不同型号的查询。我的first_query来自问题模型,第二个查询来自最喜欢的模型。我想用第二个查询结果中的user_favourite列映射到第一个查询结果。

这是我的控制器查询

def index
    @first_query = Question.order('created_at DESC').page(params[:page]).per( (ENV['ILM_QUESTIONS_PER_PAGE'] || 5).to_i )
    @second_query=Favourite.with_user_favourite(@user)
    @combined_queries = @first_query + @second_query
end

favourite.rb

scope :with_user_favourite, -> (user) {
    joins(:user).
    where(favourites: {user_id: user})
  }

index.json.builder

json.questions @combined_events

json的结果是

{
questions: [      #this is first query result
        {
            id: 88,
            user_id: 28,
            content: "test32",
            image: {
            url: null,
            thumb: {
                url: null
            },
            mobile: {
                url: null
            }
            }
        },
        {
            id: 87,
            user_id: 18,
            content: "testing riyas",
            image: {
            url: null,
            thumb: {
                url: null
            },
            mobile: {
                url: null
            }
            }
        },
        {              #this is second query result
            id: 1,
            user_id: 2,
            question_id: 84,
            created_at: "2016-05-12T06:51:54.555-04:00",
            updated_at: "2016-05-12T06:51:54.555-04:00"
        },
        {
            id: 2,
            user_id: 2,
            question_id: 81,
            created_at: "2016-05-12T07:23:47.770-04:00",
            updated_at: "2016-05-12T07:23:47.770-04:00"
        }
    ]
}

我想要回复

{
questions: [      
        {                            #first query result
            id: 88, 
            user_id: 28,
            content: "test32",
            image: {
            url: null,
            thumb: {
                url: null
            },
            mobile: {
                url: null
            }
            },
            user_favorite: {       #corresponding result from second query result
                id: 1,
                user_id: 2,
                question_id: 88
            }
        },
        {                           #first query result
            id: 87,
            user_id: 18,
            content: "testing riyas",
            image: {
            url: null,
            thumb: {
                url: null
            },
            mobile: {
                url: null
            }
            },
            user_favorite: {}       #corresponding result from second query result if there is no result for particular question in favourite table
        },
    ]
}

模型关系是:

class Question
  belongs_to :user
  has_many :favourite
end

class Favourite
  belongs_to :user
  belongs_to :question
end

class User
  has_many :questions
  has_many :favourite
end

2 个答案:

答案 0 :(得分:0)

你应该修改你的jBuilder模板以支持嵌套。因为你的模型关联就像一个问题has_many喜欢所以它将是一个数组,你可以轻松地将一个对象嵌套在另一个对象中。

 json.array! @questions do |question| 
    json.user_id question.user_id
    json.content question.content
    json.user_favorites question.favorites do |json,favorite|
       json.id question.favorite.id
       json.user_id question.favorite.user.id
       json.question_id question.id
    end 
end

以下是您可以参考的链接,以便更加清晰。

Generate a nested JSON array in JBuilder

Using JBuilder to create nested JSON output in rails

希望它有所帮助!

答案 1 :(得分:0)

您可以在questionhas_many :user_favourites 之间添加关联,以便您可以在一个问题上选择所有用户收藏。

Question.rb:

belongs_to :question

UserFavourite.rb:

def index
  @questions = Question.all.order('created_at DESC').page(params[:page]).per((ENV['ILM_QUESTIONS_PER_PAGE'] || 5).to_i)
end

然后,作为您的网络动作:

json.questions @questions do |question|
  json.user_favourites question.user_favourites
end

最后,在index.json.builder中:

jwplayer('your_div_id').setup({
    file: 'your_video_URL',
    advertising: {
        client: 'vast',
        tag: 'your_VAST_URL'
    }
});

包括您想要的任何其他字段。