lundi 30 mai 2016

How can i map one query result to another query result in ruby on rails

Hi I am new to ruby on rails development. I have two queries with different model. My first_query is get from question model and second query is get from favourite model. I want to map with a column user_favourite from second query result to first query result.

this is my controller queries

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 for the result is

{
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"
        }
    ]
}

i want response like

{
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
        },
    ]
}

Please Advice me on this issue

Thanks in Advance.

Aucun commentaire:

Enregistrer un commentaire