How would I show one of many nested objects in the index view
class Album < ActiveRecord::Base
has_many: photos
accepts_nested_attributes_for :photos,
:reject_if => proc { |a| a.all? { |k, v| v.blank?} }
has_one: cover
accepts_nested_attributes_for :cover
end
class Album Controller < ApplicationController
layout "mini"
def index
@albums = Album.find(:all,
:include => [:cover,]).reverse
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @albums }
end
end
This is what I have so fare. I just want to show a cover for each album.
Any info on this would be a massive help!!
In your view iterate over the nested data. i.e.
In your controller include the
photosin the query results.You don’t need to include the
:coverin the query as it is ahas_oneassociation(unless you are using the fields from:coverin yourWHEREcondition).I suspect you are making the
reversecall to sort the result-set. Use the:orderclause instead.OR