Rails Showing Has_Many Linked Objects Photos

Go To StackoverFlow.com

0

I can bring in Companies linked Events data, but am not able to bring in the photo that is linked to that event.

Company has_many Events & Event has_many Photos

Currently my code in Company/Show looks like

<% if @company.events.exists? then %>
<% @company.events.offset(1).each do |event| %>
 <li><h3><%= link_to "#{event.name}", {:controller => "events", :action => "show", :id => event.id}, :rel => "external" %></h3>
<p>By <%= event.artist %></p></li>
<%= image_tag event.photo.photo.url(:large) %>
<% end %>
<% else %>
  <p> There are no current events attached</p>
 <% end %>

Company Controller

 def show
   @company = Company.find(params[:id])
 end

I am using Paperclip for my Images. Any help would be much appreciated.

Code form Event Model (Keep in mind I am trying to access from Company)

 class Event < ActiveRecord::Base
  attr_accessible :name, :description, :artist, :starttime, :startdate, :finishdate,   :company_id, :photos_attributes
    belongs_to :company
    has_many :photos
     accepts_nested_attributes_for :photos, :allow_destroy => true, :reject_if => lambda { |a| a[:photo].blank? }
     accepts_nested_attributes_for :company

     end
2012-04-04 00:54
by Rupert Ralston
At the moment I have

       @event = Event.where(:company_id => :id, :order => 'DESC')
< - Rupert Ralston 2012-04-04 03:54
At the moment I have in my controler

          '@event = Event.where(:company_id => (params[:id]))'

When I call this in my Show action

'<%= image_tag @event %>'

The results is a blank image and this shows in the HTML

'#<activerecord::relation:0x102eca560> - Rupert Ralston 2012-04-04 04:01

You have not setup Paperclip properly with your Event model. Paste the code for that association and edit your question - Jatin Ganhotra 2012-04-04 05:26
Have updated the question with Event Model, thanks for looking at this - Rupert Ralston 2012-04-04 06:37
In your above comment too, you are using '<%= image_tag @event %>', which instead should be @event.photo or whatever name you are using for your Image model - Jatin Ganhotra 2012-04-04 09:07


0

You are missing the

has_attached_file :photo, :styles => {...}

part in you Event model.

Refer to this Simple Example of Paperclip, and post here if you still face any problems.

2012-04-04 09:06
by Jatin Ganhotra
Hi Jatin, thanks for the reply, unfortunately it's a little more complicated then that, Photo has it's own model as it deals with a large number of images which belong to Event, hasattachedfile is stored in the photo model which works perfectly when in Events. Company also has images which work great when in Company. It's just getting that first image from the Event database that is a pain. Do you have any other ideas - Rupert Ralston 2012-04-04 10:04
I can now see that it's fairly complicated. So, by what you are saying, photos(indirectly images) for both event and company are accessed correct. It's only when you access company-> event -> photo -> image, it fails - Jatin Ganhotra 2012-04-09 06:07
Is it only the first image that is in-accessible, or all of them? Try your hands on console, to see if every association in the above mentioned chain is correct. I can only help you this much at the moment. Happy debugging ; - Jatin Ganhotra 2012-04-09 06:09
Thanks Jatin, non of the images are accessible at the moment. I will try every association I can think of and post back if I find it. It must be in there somewhere - Rupert Ralston 2012-04-09 21:33
Ads