views:

386

answers:

1

I using the Rails resource_controller plugin. I have a ReleasesController, which is nested within a UsersController and a ProjectsController.

Resource_controller fails when it attempts to pull the release from User, but succeeds from Project. The problem under User is finding the Release results in an object of type, Enumerable::Enumerator, and not a Release. The same process to find a Release under Project results in identifying the correct Release instance.

Digging through the r_c code, I've figured out the net methods calls, which I've duplicated the problem within script/console:

>> Project.first.releases.find 17
=> #<Release id: 17, project_id: 1, name: "FORTEEN", lock_version: 10, deleted_at: nil, created_at: "2009-06-22 17:56:10", updated_at: "2009-06-22 19:48:47">

>> User.first.releases.find 17
=> #<Enumerable::Enumerator:0x599e29c>

Any Release method used on the Enumerable::Enumerator fails, of course. Here are the definitions of the releases methods:

User:
  def projects
    # active is a named_scope on Project
    employer ? employer.projects.active : Project.active
  end

  def releases
    projects.collect { |p| p.releases }.flatten
  end

Project:
  has_many :releases, :dependent => :destroy

I use the User.projects method in the ProjectsControler without difficulty. I suspect the issue lies with the definition of the User.releases method, but appreciate suggestions on how to fix.

Here are my route definitions:

  map.resources :projects do |project|
    project.resources :releases, :member => { :restore => :get }
  end

  map.resources :releases, 
                :member => { :restore => :get },
                :except => [ :new, :create ]

  map.resources :users, :member_path => '/:id', :nested_member_path =>
'/:user_id' do |user|
    user.resources :projects
    user.resources :releases, 
                   :member => { :restore => :get }, 
                   :except => [ :new, :create ]
  end

Thanks! dr

+1  A: 

My solution was to work around the difference of the return structures:

  def object
    # @object = end_of_association_chain.find(param) unless param.nil?
    # work-around r_c expectation of parent.releases result = Association proxy
    # use select block to filter out desired object rt. find
    @object ||= collection.select{ |o| o.id == param.to_i }.first unless param.nil?
    @object
  end

Not my ideal, I'd rather be able to work through the end_of_associaition_chain.find() and not override the object method.

dr