json includes with conditions

I have problems to restrict an as_json include by a dynamic attribute:

@pirates_ships = @current_account.pirates.as_json(:include => {:ships => {:only => [:id, :name]}}, :only => [:id, :last_name])

This for sure gives me all pirates with or without their ships.

But I also need to restrict the ships by eg ships.ocean_id

I tried resolving it by includes with conditions:

pirates.includes(:ships).where("ships.ocean_id = ?", @ocean.id).as_json(...)

The restriction works, but now all pirates without a ship are lost.

Also no luck with my own JOIN Syntax.

Any ideas? Ahoy

UPDATE

My solution so far is to manually eager load. This way I can have my dynamic conditions:

@pirates = @current_account.pirates
@ships = @current_account.ships.where({:pirate_id.in => @pirates, :ocean_id => @ocean.id})

render :json => { :pirates => @pirates.as_json(...), :ships => @ships.as_json(...) }

My Ajax callback can now iterate over :pirates and add for each pirate his ships if any. (I use a JS template engine clientside to generate the view from the JSON response)

Not very elegant, but performance is important in my case.

I'am still open for better ideas. I tried dynamic has_many :ships, :conditions => ... but that's a bit fiddly.


我认为你最好的选择是在从as_json生成后更改@pirates_ships散列(我尝试了多种包含等变体,并且无法获得任何工作)。

@pirates_ships = @current_account.pirates.as_json(:include => :ships)
@pirates_ships.each do |pirate|
  pirate[:ships].delete_if{ |ship| ship.ocean_id != @ocean.id }
end

# Now, @pirates_ships should contain ALL pirates, as well as ships for @ocean
链接地址: http://www.djcxy.com/p/6718.html

上一篇: 用于提高Ruby技能的紧凑型宝石或库?

下一篇: json包含条件