Jseb
Jseb

Reputation: 1934

Paperclip doesn't work showing multiple has_attached_model

I have a model called advertiser and it 5 url for image upload

Here the model

  attr_accessible :link, :publishoff, :publishon, :title, :adone, :adtwo, :adthree, :adfour, :adfive
  has_attached_file :adone, :styles => {
        :small => "150x150>",
        :medium => "300x300>",
        :thumb => "100x100>"
    },
    :url  => "/assets/advertiser/adone/:id/:style/:basename.:extension",
    :path => ":rails_root/public/assets/advertiser/adone/:id/:style/:basename.:extension"

Where adone adtwo are my image upload and so on, all have the same idea of upload.

Now the view looks like this

8: <% unless @advertisments.nil? %>
9: <%= link_to image_tag(@advertisments.adone.url(:small), :title =>"#{@advertisments.title}"), @advertisments.link, :target => "_blank" %>
10: <% end %>

And here my controller application_controller

@advertisments = Advertiser.where("publishon <= ? AND publishoff >= ?", Date.today, Date.today).limit(1)

The error i get is the following

undefined method `adone' for #<ActiveRecord::Relation:

Upvotes: 0

Views: 76

Answers (1)

Substantial
Substantial

Reputation: 6682

Your controller method is returning an ActiveRecord::Relation collection... the object you want is inside of it.

Add .first to the end of this line, like so:

@advertisments = Advertiser.where("publishon <= ? AND publishoff >= ?", Date.today, Date.today).limit(1).first

This will return the only Advertiser from within the collection and assign it to @advertisements.

Upvotes: 1

Related Questions