1

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

The model relationships are:

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
Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
Riyas khan
  • 31
  • 4
  • What is the relation between Question model, Favorite model and User model? – Hiren Bhalani May 31 '16 at 07:06
  • Add your associations – Thorin May 31 '16 at 07:14
  • in question.rb belongs_to :user has_many :favourite in favourite.rb belongs_to :user belongs_to :question in user.rb has_many :questions has_many :favourite – Riyas khan May 31 '16 at 07:19
  • You're either using rails 3 or rails 4 - not both. Run `rails --version` - what version are you using? – ArtOfCode May 31 '16 at 11:49
  • @Riyaskhan You can use the `edit` button below the Question (directly above these comments) to add new information to your question. Please do this, instead of pasting code into comments. I've placed your model relationships in the question, and you should do the same with any additional information that you include. – Michael Gaskill May 31 '16 at 15:08

2 Answers2

0

You should modify your jBuilder template to support nesting.Since your model association is like one question has_many favorite so it will be an array and you can easily nest one object inside another.

 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

Here is a link that you can refer to for more clarity.

Generate a nested JSON array in JBuilder

Using JBuilder to create nested JSON output in rails

Hope it helps!.

Community
  • 1
  • 1
Saroj
  • 1,551
  • 2
  • 14
  • 30
  • i got this error .undefined method `favorites' for # – Riyas khan May 31 '16 at 10:36
  • As you said in question.rb Model you have following association "has_many" :favourite. It should not show that error.@ Riyas can you please post the association? May be it can help us to find if cause. – Saroj May 31 '16 at 11:07
0

You can add an association between user_favourite and question so that you can select all user favourites on one question.

Question.rb:

has_many :user_favourites

UserFavourite.rb:

belongs_to :question

Then, as your web action:

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

And finally, in index.json.builder:

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

including whatever other fields you want.

ArtOfCode
  • 5,702
  • 5
  • 37
  • 56