Is it possible to order included models?
format.json { render :json => #client.to_json(:include => { :videos => { :order => 'sortorder' } }) }
You need made when you create your #client. In your SQL request
Related
this function works. However, instead of including all the rounds. I just need to include the last one. How would I do it? I looked into using :scope but haven't found anything useful. Please help, it's driving me nuts!
def get_games_list
Rails.logger.info("get_games_list".colorize(:color => :white, :background => :blue))
player_1_games = Game.includes(:rounds, :player_2).where(:player_1_id => params[:player_id], :is_valid => true)
player_2_games = Game.includes(:rounds, :player_1).where(:player_2_id => params[:player_id], :is_valid => true)
render json: {
error_code: ERROR_NOERROR,
status: "ok",
player_1_games: player_1_games.as_json(
:include => {
:player_2 => {
:only => [:id, :user_name]
},
:rounds => {
:only => [:id, :playing_player_id]
}
}
),
player_2_games: player_2_games.as_json(
:include => {
:player_1 => {
:only => [:id, :user_name]
},
:rounds => {
:only => [:id, :playing_player_id]
}
}
)
}
return
end
You could create a :last_round has_one association in the Game model.
class Game < ApplicationModel
# ...
has_one :last_round, -> { order(round_date: :desc) }, class_name: 'Round'
# ...
end
This is what I have:
def index
#attachments = current_user.attachments.all
respond_to do |format|
format.json do
render :json => #attachments.map { |o| { url: o.picture.thumb.url }}
end
end
end
=> [{:url=>"/uploads/attachment/picture/7/thumb_df3c0c3c.jpg"}, {:url=>"/uploads/attachment/picture/12/thumb_dd7839ee.jpg"}, ... }]
How can I change the key from :url to :thumb?
=> [{:thumb=>"/uploads/attachment/picture/7/thumb_df3c0c3c.jpg"},
{:thumb=>"/uploads/attachment/picture/12/thumb_dd7839ee.jpg"}, ... }]
This is the whole object after: render :json => #attachments
My goal: thumb: thumb: "/uploads/attach..."
Background: https://www.froala.com/wysiwyg-editor/docs/concepts/image-manager
I use the gem carrierwave to create a thumb
response.map! { |urls| { :thumb => urls[:url] } }
change key from "url" to "thumb"
render :json => #attachments.map { |o| { **thumb: o.picture.thumb.url** }}
im trying to add some methods inside some includes with as_json in rails, but the second includes with method doesnt shows on the json. this is the code
format.json { render :json => { :projects => #projects.as_json(:include => [
:retail,
:property => {:methods => :name},
:user => {:methods => [:name, :address] } ] ) } }
and this is the json generated:
{"projects":[{"comment":null,"contractor":1,"created_at":"2013-10-17T16:57:36Z","id":5,"property_id":3,"retail_id":1,"updated_at":"2013-10-17T16:57:36Z","user_id":2,"retail":{"address":null,"created_at":"2013-10-16T22:29:38Z","id":1,"name":"asdasdsa","phone":"6677969224","property_id":null,"social_reason_id":null,"updated_at":"2013-10-17T17:18:09Z","web":"www.boxie.io"},"property":{"city":"Culiacan","colony":"Fracc Los Sauces","coordinates":"(24.7802016, -107.34894730000002)","country_id":1,"created_at":"2013-10-17T00:56:32Z","external_number":null,"id":3,"internal_number":2848,"population":null,"state_id":4,"street":"Alame\u00f1a","updated_at":"2013-10-17T00:56:32Z","zipcode":8028,"name":"Calle Alame\u00f1a, Fracc Los Sauces, 8028, Culiacan, Campeche Mexico"}}]}
as you can see the user information doesn't appear on the json.
Thank you
You didn't wrap them correctly, Your second and third child association should be hashes.
#projects.as_json(:include => [
:retail,
{ :property => {:methods => :name } },
{ :user => {:methods => [:name, :address] } }
]
I've got two models that are connected through a third one:
Playlist has_many :song_in_playlists, :dependent => :destroy
has_many :songs, :through => :song_in_playlists
Song has_many :song_in_playlists, :dependent => :destroy
has_many :playlists, :through => :song_in_playlists
The song_in_playlist table simply lists the playlist_id, song_id, as well as a track_number.
When calling /playlists/1.json, I'd like to get a json hash that holds the playlist's attributes and has another hash inside, which contains the songs (as in /songs.json plus the track_number from the song_in_playlists table entry).
According to the Rails API, I should be able to achieve this doing the following:
def show
#playlist = Playlist.find(params[:id])
[...]
respond_to do |format|
format.html # show.html.erb
format.json { render json: #playlist.as_json(
:include => { :song_in_playlists => {
:only => :track_number, :include => :songs {
:except => [:dropbox_link, :local_path] } } }
) }
end
end
end
However, what I do get is the following:
/home/sloth/audiomixer/app/controllers/playlists_controller.rb:30: syntax error, unexpected '{', expecting '}'
:only => :track_number, :include => :songs {
^
/home/sloth/audiomixer/app/controllers/playlists_controller.rb:31: syntax error, unexpected '}', expecting keyword_end
:except => [:dropbox_link, :local_path] } } }
^
/home/sloth/audiomixer/app/controllers/playlists_controller.rb:103: syntax error, unexpected $end, expecting keyword_end
So I tried a little less complexity, calling
format.json { render json: #playlist.as_json(
:include => song_in_playlists {
:only => :track_number } ) }
but I'm getting a similar error. As soon as I'm putting any braces in between the parentheses, Rails complains, although the API documentation states the opposite.
I really don't know what to try anymore :(
PS: Ruby 1.9.3, Rails 3.2.13
--- EDIT ---
Okay, I don't really know why but I got this to produce the expected output:
format.json { render json: #playlist.as_json(
:include => { :song_in_playlists => {
:include => :song, :only => :track_number } } ) }
However, when trying to add { :except => [:dropbox_link, :local_path] } in between :song and , :only, I'm getting the same errors again..
This is only partly about rails
:include => song_in_playlists {:only => :track_number}
just isn't valid ruby syntax
this should be
:include => {:song_in_playlists => {:only => :track_number}}
Each time, the key is the association name (song_in_playlists) and the value is hash containing one or more of the keys :only, :except or :include
If you want to change what attributes from the songs table are in the json then you need to take
format.json { render json: #playlist.as_json(
:include => { :song_in_playlists => {
:include => :song, :only => :track_number } } ) }
and replace :song with a hash that describes what you want to do with song, for example
format.json { render json: #playlist.as_json(
:include => {
:song_in_playlists => {
:include => {
:song => {
:except => [:dropbox_link, :local_path]
}
},
:only => :track_number
}
})}
Hello i"m trying desperately to render json that includes methods within my included nested resources. I tried many variants but just can't get that thing to run.
This is what i have:
format.json {render json: #user, :include => [ :votes, :petitions, :roles ] }
And that is what i had hoped to work
format.json {render json: #user, :include => {
:votes => { :methods => [ :status, :count_users_voted ] },
:petitions => { :methods => [:status, :count_users_voted] },
:roles
}
}
Any hints anyone?
Add something like this to your User model:
def as_json(options = { })
super((options || { }).merge({
:methods => [:agrees, :disagrees]
}))
end
def agrees
self.liked_by_count
end
def disagrees
self.disliked_by_count
end
Pretty straight forward, I hope this helps