class TodosController < ApplicationController
  def show
    @todo = Todo.find(params[:id])
    respond_to do |format|
      format.json { render_for_api :public, :json => @todo }
    end
  end
end

class Todo < ActiveRecord::Base
  has_many items

  acts_as_api

  api_accessible :public do |t|
    t.add "items.finished_items.all", :as => finished_items
    t.add "items.unfinished_items.all", :as => unfinished_items
    t.add "items.finished_items.count", :as => num_of_finished_items
    t.add "items.unfinished_items.count", :as => num_of_unfinished_items
  end
end

class Item < ActiveRecord::Base
  belongs_to todo

  scope :finished_items, where(:status => 'finished')
  scope :unfinished_items, where(:status => 'unfinished')

  acts_as_api
end

On Mon, Aug 1, 2011 at 11:49 PM, Linus Pettersson <
[email protected]> wrote:

> Well, yes but that's not the point.
>
> Items have a status field. Lists has many items.
> When displaying a List as JSON I want it to have an attribute that counts
> how many finished items there are in the list and how many unfinished items
> there are in the list. The above methods only fetches the items, and I'd
> have to count the result of that of course.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/gy0mFW1iV44J.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>



-- 
Tower He

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to