Correctly displaying nested models in my show view

2019-05-24 14:11发布

问题:

I'm new to rails and I'm slightly confused on how to properly display nested model attributes in a view.

I'm using Rails 3.2.6.

My 3 models here:

class Company < ActiveRecord::Base
  attr_accessible :name, :vehicles_attributes, :engines_attributes
  has_many :vehicles, :dependent => :destroy
  accepts_nested_attributes_for :vehicles, :allow_destroy => true,
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

class Vehicle < ActiveRecord::Base
  attr_accessible :company_id, :engines_attributes

  belongs_to :company

  has_many :engines, :dependent => :destroy

  accepts_nested_attributes_for :engines, :allow_destroy => true,
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

class Engine < ActiveRecord::Base
  attr_accessible :make, :model, :model_year, :vehicle_id

  belongs_to :vehicle

end

I have these models in a nested form using simple_form_for and simple_fields_for partials. Here the companies_controller.rb

  def show
    @company = Company.includes(vehicles: :engines).find(params[:id])
    #added this, thanks to @achempion
  ...
  end

  def new
    @company = Company.new
    @company.addresses.build 
    @company.vehicles.build
    ...
  end

my show view:

  <% for vehicle in @company.vehicles %>
      <p>Make: <strong><%= h vehicle.make %></strong></p>
      <p>Model: <strong><%= h vehicle.model %></strong></p>
      <p>Odometer Reading: <strong><%= h vehicle.odometer %></strong></p>
      <p>Vehicle ID No. (VIN): <strong><%= h vehicle.vin %></strong></p>
      <p>Year: <strong><%= h vehicle.year %></strong></p>
  <% end %>

but how do I reference company -> vehicles -> engines in a loop like I do with vehicles? I'm always open to suggestions!

Currently I've tried a bunch of syntax with @company.vehicles.engines but I keep getting

undefined method `engines' for #<ActiveRecord::Relation:0x007fd4305320d0>

I'm sure it's just me being unfamiliar on proper syntax in the controller and also in the show view.

Any help is appreciated =)

Also, is there maybe a better way to do that loop? maybe

<%= @company.vehicles.each |vehicle| %>
 <%= vehicle.make %>
 ...
<% end %>

?? :)

回答1:

I thik that @company = Company.includes(vehicles: :engines).find(params[:id]) it's a good way. see more here

And your main model must forget has_many :engines, :dependent => :destroy because it's for Vehicle model. Good luck!

and you also can see engines as:

@company.vehicles.each do |vehicle|
  vehicle.engines.each do |engine|
    puts engine.id
  end
end

edit: fixed small typos.