Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

How could I have worked this out for myself?

So, I completed the One Month Rails tutorial (Horray!): http://www.aspinteresting.com

And I decided that the images weren't big enough when clicking through to them.

So I changed the paperclip thingy to create a :large version when uploading of certain dimensions.

So, at this point, the database had medium versions of the previously uploaded photos, and large AND medium versions of new photos that were uploaded, after this modification was made.

So, in the view, I had to write an 'if else' function to display the large photo if it existed, else the medium photo.

I only managed to work out how to do this by googling, and discovering:

<% if @pin.image.exists?(:large) %>

...namely, the 'exists?' method. Also, following it with ':large'.

The rest of the section is:

<%= image_tag @pin.image.url(:large), class: "img-responsive"%>
        <% else %>
        <%= image_tag @pin.image.url(:medium) %>
        <% end %>

That part I understand - I wrote that myself :-D.

What I'm saying is, I don't know WHY this works, and I want to know.

I don't like the idea of googling my way through my learning.

I need to understand what I'm doing.

So, could somebody help me to TRULY understand what's going on here?

I understand that @pin is an instance variable, but the rest of it I'm pretty shaky on.

Here's the line again:

<% if @pin.image.exists?(:large) %>

By the way, this is not just about what this line means, it's also about HOW COULD I HAVE FOUND THIS INFORMATION? That is, what documentation contains information about the 'exists?' method? Where would I find it?

1 Answer

Jim Hoskins
STAFF
Jim Hoskins
Treehouse Guest Teacher

This is a good question.

Sadly, I can't just point at some great documentation, and tell you it's easily found and understood. Sometimes the documentation our tools has is less than ideal, if it exists at all. In this case, I couldn't find documentation that would have been obvious.

If I had encountered this problem, I'd have done what you did, google it. It seems you found your solution that way, so well done!

But, can we find out what .exists? does. Well, let's check out the paperclip website: it's github page https://github.com/thoughtbot/paperclip

The README gives a lot of good information and examples. It's worth reading to understand better how Paperclip works. However, the exists? method isn't documented here. So without finding out about it with google, it would be more work to know that it exists.

They link to the RDoc documentation on the README, so let's see if it's there. http://www.rubydoc.info/gems/paperclip

After reading about it, it seems the object you are calling .exists? on (in this case @pin.image) is probably an instance of Attachment, so let's click on it's docs. http://www.rubydoc.info/gems/paperclip/Paperclip/Attachment

This looks like the right class, we can see the docs for methods like .url(). Hmmm, exists? is nowhere to be found. Well, let's find it.

In the sidebar, we want to click "methods", then we can search for "exists?". We find it in 3 Modules:

Paperclip::Storage::S3, Paperclip::Storage::FileSystem and Paperclip::Storage::Fog

So it seems the exists? method is defined with the storage adapters, likely because the implementation of exists? differs based on the storage system. You can click to see the docs, but there's not much more than the argument and return value.

What's happening is one of these modules gets "mixed in" to the Attachment object, so the attachment gains those methods.

So the bottom line is, exists? wasn't well documented as a feature. These types of things you either discover by googling, thoroughly reading the docs, or even by diving into the source code, or inspecting the methods in the code itself.

Hope that helps

That does help a lot. Thankyou very much, Jim.

Could you explain what the :large means exactly? I understand that ":large" is a symbol, and I kind of understand the different between using symbols instead of strings as names for things.

However, I'm not really sure what ':large' is, exactly.

In fact, what are all of the symbols in this piece of code? I'm not used to the 'shorthand' way of writing Ruby. In the Treehouse tutorials, so far it's been mostly long-hand, which I find easier to understand.

class Pin < ActiveRecord::Base
     belongs_to :user
     has_attached_file :image, :styles => { :large => "x600>", :medium => "300x300>", :thumb => "100x100>" }
     validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

     validates :image, presence: true
end

I think :large is a hash key, :styles is the name of the hash, but I'm not sure what :image or :user represents.

Github: https://github.com/Yorkshireman/pinteresting

Thanks