Sunday, April 17, 2011

[Rails] 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


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate