Newbie: to_json only/except + association confusion

hi,

i want to return to_json with ‘only’ and also filtered.so i tried
various ways, but none of them are really good:

A)#OK , but filter missing
format.json { render :json => @project.to_json(:only =>
[:title ], :include=> { :tasks => {:only=>[:id,:title] } } ) }

B)#Not OK > filter does not work,
format.json { render :json => @project.to_json(:only =>
[:title ], :include=> { :tasks => {:only=>[:id,:title] }, :conditions
=> [‘tasks.is_enabled = ?’, 1] } ) }

C) #Not OK > works, but cant use only/except
format.json { render :json => @project.to_json(:only =>
[:title ], :method=> get_enabled_tasks ) }

def get_enabled_tasks
tasks.where(“is_enabled = ?”, 1)

D)#OK, but json output not nice
format.json { render :json => @project.to_json(:only =>
[:title ], :method=> get_enabled_tasks ) }

def get_enabled_tasks
ActiveRecord::Base.connection.execute(sql)

“project”:{“title”:“testprojecttitle”,“get_enabled_tasks”:
[[“tasktitle”,“user1”,“1.1.2011”]]}}

so, how can i get filtered associations in a to_json call so that i actually
get goodlookin json?

thx

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs