<p>I am using running a simple find all and paginating with willpaginate, but I'd also like to have the query sorted by the user. The first solution that came to mind was just use a params[:sort]</p>

<pre><code>http://localhost:3000/posts/?sort=created_at+DESC

@posts = Post.paginate :page =&gt; params[:page], :order =&gt; params[:sort]
</code></pre>

<p>But the problem with his approach is that the query is defaulting as sorting by ID and I want it to be created_at. </p>

<p>Is this a safe approach to sorting and is there a way to default to created_at?</p>

In general, the way to supply default values for Hash and Hash-like objects is to use
```
fetch
```
:
```
```
params.fetch(:sort){ :created_at }
```
```
A lot of people just use
```
||
```
though:
```
```
params[:sort] || :created_at
```
```
I prefer
```
fetch
```
myself as being more explicit, plus it doesn't break when
```
false
```
is a legitimate value.