views:

56

answers:

1

I seem to be getting error:

uninitialized constant Style::Pic

when I'm trying to render a nested object in to the index view the show view is fine.

class Style < ActiveRecord::Base
#belongs_to :users
has_many :style_images, :dependent => :destroy
accepts_nested_attributes_for :style_images,
 :reject_if => proc { |a| a.all? { |k, v| v.blank?} } #found this here http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

has_one :cover, :class_name => "Pic", :order => "updated_at DESC"
accepts_nested_attributes_for :cover
end


class StyleImage < ActiveRecord::Base
belongs_to :style
#belongs_to :style_as_cover, :class_name => "Style", :foreign_key => "style_id"
has_attached_file :pic, 
                  :styles => { :small => "200x0>", 
                               :normal => "600x> " }

validates_attachment_presence :pic
#validates_attachment_size :pic, :less_than => 5.megabytes

end



<% for style_image in @style.style_images %>
<li><%= style_image.caption %></li>


<div id="show_photo">


    <%= image_tag style_image.pic.url(:normal) %></div>

<% end %>

As you can see from the above The main model style has many style_images, all these style_images are displayed in the show view but, in the the index view I wish to show one image which has been name and will act as a cover that is displayed for each style.

in the index controller I have tried the following:

class StylesController < ApplicationController
  layout "mini"
  def index
    @styles = Style.find(:all,
    :inculde => [:cover,]).reverse

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @styles }
    end
  end

and the index

<% @styles.each do |style| %>


<%=image_tag style.cover.pic.url(:small) %>

<% end %>



class StyleImage < ActiveRecord::Base
belongs_to :style
#belongs_to :style_as_cover, :class_name => "Style", :foreign_key => "style_id"
has_attached_file :pic, 
                  :styles => { :small => "200x0>", 
                               :normal => "600x> " }

validates_attachment_presence :pic
#validates_attachment_size :pic, :less_than => 5.megabytes

end

In the style_images table there is an cover_id also.

From the about you can see that I have included the cover in the controller and the model. I have know idea where I'm going wrong here!

If any one can help please do!

A: 

You have to fix your :cover association definition as follows.

has_one :cover, :class_name => "StyleImage", :order => "updated_at DESC"

I see another potential issue in your design. You have a has_many and a has_one association pointing to the same table using the same foreign key (style_id).

class Style < ActiveRecord::Base
  has_many :style_images
  has_one  :cover, :class_name => "StyleImage", :order => "updated_at DESC"
end

For the :cover association to work, the cover image has to have the max updated time in the style_images table. This is not a very safe assumption to make. You can improve this as follows:

Add a new column to style_images table to store the image type. Now your association can be rewritten as:

has_one  :cover, :class_name => "StyleImage", :conditions => {:itype => "cover"}

OR

Change the has_one association to belongs_to and store the foreign key(style_image_id) in the styles table, i.e.

class Style < ActiveRecord::Base
  has_many :style_images
  belongs_to  :cover, :class_name => "StyleImage"
end
KandadaBoggu
Pic is in the style_images table in the database.
MrThomas