I want json output order by id(child id)
Can I solve this problem ?
this is some code in my project
show.json.rabl (I used Rabl)
object @book
attributes :id , :name
child :questions do
attributes :id , :name
end
books_controller.rb
def show
@book = Book.find(params[:id])
end
Sorry for my English , Thank you.
It depends on your app and what you want to accomplish, but you could define a default_scope
in the Question
model like this:
class Question < ActiveRecord::Base
default_scope order('id ASC')
end
Or you could define a default_scope
in the Book
model:
class Book < ActiveRecord::Base
default_scope joins(:questions).order('questions.id ASC')
end
If you want eager load the questions
, then use includes
instead of join
.
i don't know RABL, but i think that you can just pass in a collection instead of a symbol. given, that you :question
is a has_many
relation on your Book
class, you could just use a finder for that:
child @book.questions.order('id ASC') do
attributes :id , :name
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With