# Conditional Get

**URL:** <https://www.ruby-forum.com/t/conditional-get/166082>\
**Category:** Rails\
**Created:** [April 21, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/conditional-get/166082 "2009-04-21T19:08:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [April 21, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/conditional-get/166082/1 "2009-04-21T19:08:55Z")

</div>

Does anyone have any suggestions on how to create Conditional Get  
requests in rails? I understand the basic philosophy of the concept,  
I was just wondering if there was an easy way (a helper or plugin) to  
automatically generate the proper http headers when checking on  
whether a file has been updated on a server. Ideally, I would like to  
generate a conditional get in an if statement, followed by the caching  
algorithm if the conditional get returns the document.

Thanks!

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [April 21, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/conditional-get/166082/2 "2009-04-21T23:07:33Z")

</div>

Is RESTful Rails the answer?

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [April 21, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/conditional-get/166082/3 "2009-04-21T23:27:29Z")

</div>

Yep, there are built in functionality to support conditional requests  
(bot etags and last modified). Just use it in the controller action:

def some\_action  
if stale?(:last\_modified =\> @post.updated\_at.utc, :etag =\> @post)  
respond\_to do |format|  
# generate a full response  
end  
end

# otherwise cached will be used

end

See:  
[http://ryandaigle.com/articles/2008/10/25/what-s-new-in-edge-rails-even-better-conditional-get-support](http://ryandaigle.com/articles/2008/10/25/what-s-new-in-edge-rails-even-better-conditional-get-support)

Dmitry

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [April 28, 2009, 6:55pm UTC](https://www.ruby-forum.com/t/conditional-get/166082/4 "2009-04-28T18:55:00Z")

</div>

Thanks for the conditional\_get tip.

When looking at the api for stale? and fresh\_when, I see that these  
methods are part of action controller. I would like to use this  
methodology to determine if an rss feed should be updated or not. The  
validation process all resides in the Feed model and a related module,  
so when I try to use the stale? method, I get an undefined method  
error. Would it be better to recreate the source code in my module,  
or is this the first indication that what I am trying to do is going  
about it the wrong way? I would very much like to keep the process in  
a module, as I am using a template pattern to define several different  
caching methods for experiments on speed and agility.

Any suggestions or advice you may have on this subject would be  
greatly appreciated!

Thanks!

Jay

On Apr 21, 5:13 pm, Dmitry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [April 28, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/conditional-get/166082/5 "2009-04-28T19:01:27Z")

</div>

In short — you’re doing it in the wrong way. Caching should be handled  
on the controller layer, unless you really need something advanced  
(and I don’t think you need it). So rethink the way your app is  
designed.

Dmitry

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [April 28, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/conditional-get/166082/6 "2009-04-28T21:31:36Z")

</div>

I agree, and appreciate your insight.

I want to be the RECIPIENT of either the “304 Not Modified” result, or  
the document if it has been modified. If I am reading the  
documentation properly, It is describing ways to handle a request and  
return the message, so I am looking for the other end of the stick.

I have been successful at generating my desired results using open-  
uri. Detailed information is available here:

> **[RESTful Web Services](https://books.google.com/books?id=XUaErakHsoAC&pg=PA244&dq=open-uri%2Bconditional%2Bget&hl=en)**
>
> "Every developer working with the Web needs to read this book." -- David Heinemeier Hansson, creator of the Rails framework "RESTful Web Services finally provides a practical roadmap for constructing services that embrace the Web, instead of trying...

around page 245.

Thanks for your help.

Jay

On Apr 28, 1:00 pm, Dmitry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
