SOLVED: Note to future self:
On Aug 9, 2012, at 1:39 PM, Walter Lee D. wrote:
end
class Foo < ActiveRecord::Base
attr_accessible :name, :bars_attributes
has_many :bars
has_many :bazs, :through => :bars, :dependent => :destroy
accepts_nested_attributes_for :bars
end
#bar.rb
class Bar < ActiveRecord::Base
belongs_to :foo
belongs_to :baz
also defines the ‘blarg’ attribute
end
class Bar < ActiveRecord::Base
attr_accessible :baz_id, :blarged, :foo_id, :baz_attributes, :baz
belongs_to :baz
belongs_to :foo
accepts_nested_attributes_for :baz
end
#baz.rb
class Baz < ActiveRecord::Base
has_many :bars
has_many :foos, :through => :bars
end
class Baz < ActiveRecord::Base
attr_accessible :title
has_many :bars
has_many :foos, :through => :bars, :dependent => :destroy
end
<h2><%= pluralize(@foo.errors.count, "error") %> prohibited this foo from
being saved:
<%= f.label :name %>
<%= b.check_box :blarged %>
<% end %>
<%= f.submit %>
<% end %>
<%= form_for(@foo) do |f| %>
<% if @foo.errors.any? %>
<%= pluralize(@foo.errors.count, "error") %> prohibited this foo
from being saved:
<% @foo.errors.full_messages.each do |msg| %>
- <%= msg %>
<% end %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :bars do |b| %>
<%= b.fields_for :baz do |z| %>
<%= z.label :title, "Baz" %>
<%= z.text_field :title %>
<% end %>
<%= b.check_box :blarged %>
<% end %>
<%= f.submit %>
<% end %>
1.9.2p320 :004 > f.bazs
from script/rails:6:in require' from script/rails:6:in
’
1.9.2p320 :006 >
It seems to me as though if I use the magical h/m/t and nested form behavior, I
don’t get access to the specific bar being used to bridge one foo to one baz. If I
manually inspect a foo’s bars, I can see the extra attribute (but I would have to
rewrite my form and controller and not use the magic). What’s the trick I need?
The trick was to not try to build a bridge in mid-air. The foo could
find a baz, but that found baz did not seem to have any notion of which
bar was standing between the two. Nesting my forms one layer at a time
did the trick. fields_for bars then contained fields_for baz.
Walter