Correctly displaying nested models in my show view

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 %>

?? :)

Answers


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.


Need Your Help

Private composer package refuses to update

git symfony2 composer-php

At work, I have a GitLab setup as a Git repository to host a common composer package that can and will be used across multiple sites (a symfony2 bundle for what it's worth). Not a totally uncommon

How to search for record in mysql using php?

php mysql database search phpmyadmin

Thanks for all your help, I now have add, update, and delete functions on my database manipulation program.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.