I have two models with a straightforward has_many / belongs_to
relationship:

class Premise < ActiveRecord::Base
  has_many :metered_services, :dependent => :destroy
  ...
end

class MeteredService < ActiveRecord::Base
  belongs_to :premise
  ...
end

and nested routes to match:

Demo::Application.routes.draw do
  devise_for :users
  resources :premises do
    resources :metered_services
  end
  ...
end

I want to show/edit the metered services on the same page as premise
edit page, so my views/premises/edit.html.erb file has this line:

<% Rails.logger.debug("== #{@premise.address} has
#{@premise.metered_services.count} metered services") %>
<%= render :partial => "metered_services/metered_service", :collection
=> @premise.metered_services %>

and my stubbed views/metered_services/_metered_service.html.erb is just
this:

<% Rails.logger.debug("== _metered_service: metered_service =
#{metered_service.inspect}") %>

So here's what's weird: With a newly created premise -- before I've
added any metered services -- the _metered_service.html.erb partial gets
called with a metered service object whose ID is nil and whose
premise_id field is filled in with the owning premise.  But there should
be ZERO metered services, not one.  The console will show something
like:

== 1800 Pennsylvania Avenue has 0 metered services
  MeteredService Load (0.2ms)  SELECT "metered_services".* FROM
"metered_services" WHERE ("metered_services".premise_id = 48)
== metered_service = #<MeteredService id: nil, premise_id: 48, ... >

Is this a special "feature" of render ... :collection?  I can always
filter out metered_services with nil ids in the partial, but that seems
really odd.  Am I doing something wrong?

- ff

-- 
Posted via http://www.ruby-forum.com/.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to