# Named Scopes and Created At

**URL:** <https://www.ruby-forum.com/t/named-scopes-and-created-at/180958>\
**Category:** Rails\
**Created:** [January 2, 2010, 2:36am UTC](https://www.ruby-forum.com/t/named-scopes-and-created-at/180958 "2010-01-02T02:36:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 2, 2010, 2:36am UTC](https://www.ruby-forum.com/t/named-scopes-and-created-at/180958/1 "2010-01-02T02:36:10Z")

</div>

I am using named scopes in my Post model to find recently published  
posts:

named\_scope :recently\_published, :conditions =\> { :published =\>  
true }, :order =\> ‘created\_at DESC’

Posts HABTM tags, so I have my routes for those setup as:

map.resources :tags do |tag|  
tag.resources :posts  
end

So that will give me a route to use, such as:  
tag\_posts GET /tags/:tag\_id/posts(.:format)  
{:action=\>“index”, :controller=\>“posts”}

For the view, I find posts like this from the Posts controller:  
@tag = Tag.find(params[:tag\_id])  
@posts = @tag.posts.recently\_published

Everything works as expected except the created\_at attribute for my  
posts. When I print the created at timestamp in the view, it is  
actually printing the created timestamp for the tag.

\<%= post.created\_at.strftime(‘%B %d, %Y’) %\> gives me the created at  
timestamp for the tag, not the post.

Can someone help me fix this issue? Thank you!

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 2, 2010, 12:00pm UTC](https://www.ruby-forum.com/t/named-scopes-and-created-at/180958/2 "2010-01-02T12:00:19Z")

</div>

On Jan 2, 1:34 am, jm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Everything works as expected except the created\_at attribute for my  
> posts. When I print the created at timestamp in the view, it is  
> actually printing the created timestamp for the tag.
> 
> \<%= post.created\_at.strftime(‘%B %d, %Y’) %\> gives me the created at  
> timestamp for the tag, not the post.
> 
> Can someone help me fix this issue? Thank you!

By default when you access a habtm relationship like this rails does

select \* from posts  
inner join post\_tags on …  
…

So if the join table in the middle has an attribute with the same name  
as an attribute from joined table(s) they will collide. You can avoid  
this by adding :select =\> ‘posts.\*’ to your habtm declaration

Fred

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 2, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/named-scopes-and-created-at/180958/3 "2010-01-02T17:02:20Z")

</div>

Sorry I have no time to test this, but try this:

named\_scope :recently\_published, :conditions =\> { :published =\>  
true }, :order =\> ‘posts.created\_at DESC’

I am pretty sure you can be specific about the table in this case.

CowboyonRails  
Chris Reister

On Jan 1, 5:34 pm, jm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> posts. When I print the created at timestamp in the view, it is  
> actually printing the created timestamp for the tag.
> 
> \<%= post.created\_at.strftime(‘%B %d, %Y’) %\> gives me the created at  
> timestamp for the tag, not the post.
> 
> Can someone help me fix this issue? Thank you!

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 3, 2010, 6:15pm UTC](https://www.ruby-forum.com/t/named-scopes-and-created-at/180958/4 "2010-01-03T18:15:56Z")

</div>

Thanks for the replies. Unfortunately, neither of the solutions  
worked. I tried each on their own with no luck, and then I combined  
the two suggestions and still no luck. Here is what I have for the  
named scope in my Post model as of now:

named\_scope :recently\_published, :conditions =\> { :published =\>  
true }, :select =\> ‘posts.\*’, :order =\> 'posts.created\_at

Not sure if it helps, but here are the queries used to generate the  
page at: [http://localhost:3000/tags/1/posts](http://localhost:3000/tags/1/posts)

Processing PostsController#index (for ::1 at 2010-01-03 11:13:21)  
[GET]  
Parameters: {“tag\_id”=\>“1”, “action”=\>“index”,  
“controller”=\>“posts”}  
Tag Load (0.2ms) SELECT \* FROM “tags” WHERE (“tags”.“id” = 1)  
Rendering template within layouts/application  
Rendering posts/index  
SQL (0.2ms) SELECT count(_) AS count\_all FROM “posts” INNER JOIN  
“posts\_tags” ON “posts”.id = “posts\_tags”.post\_id WHERE  
(((“posts”.“published” = ‘t’) AND (“posts\_tags”.tag\_id = 1 )) AND  
(“posts\_tags”.tag\_id = 1 ))  
Post Load (0.4ms) SELECT \* FROM “posts” INNER JOIN “posts\_tags” ON  
“posts”.id = “posts\_tags”.post\_id WHERE (“posts\_tags”.tag\_id = 1 ) AND  
((“posts”.“published” = ‘t’) AND (“posts\_tags”.tag\_id = 1 )) ORDER BY  
posts.created\_at DESC  
Category Load (0.2ms) SELECT \* FROM “categories” WHERE  
(“categories”.“id” = 2)  
Tag Load (0.2ms) SELECT \* FROM “tags” INNER JOIN “posts\_tags” ON  
“tags”.id = “posts\_tags”.tag\_id WHERE (“posts\_tags”.post\_id = 2 )  
Category Load (0.2ms) SELECT \* FROM “categories” WHERE  
(“categories”.“id” = 1)  
Tag Load (0.4ms) SELECT \* FROM “tags” INNER JOIN “posts\_tags” ON  
“tags”.id = “posts\_tags”.tag\_id WHERE (“posts\_tags”.post\_id = 1 )  
Category Load (0.5ms) SELECT \* FROM “categories” ORDER BY name ASC  
LIMIT 5  
SQL (0.1ms) SELECT count(_) AS count\_all FROM “categories”  
Post Load (0.3ms) SELECT posts.\* FROM “posts” WHERE  
(“posts”.“published” = ‘t’) ORDER BY posts.created\_at DESC LIMIT 5  
Tag Load (0.3ms) SELECT \* FROM “tags” ORDER BY name ASC LIMIT 5  
SQL (0.1ms) SELECT count(\*) AS count\_all FROM “tags”  
Rendered layouts/\_side\_navigation (5.8ms)  
Completed in 86ms (View: 20, DB: 3) | 200 OK [[http://localhost/tags/1/](http://localhost/tags/1/)  
posts]

Thanks again for the help and suggestions!

On Jan 2, 10:00 am, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 1, 5:34 pm, jm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > @posts = @tag.posts.recently\_published
> 
> > Everything works as expected except the created\_at attribute for my  
> > posts. When I print the created at timestamp in the view, it is  
> > actually printing the created timestamp for the tag.
> 
> > \<%= post.created\_at.strftime(‘%B %d, %Y’) %\> gives me the created at  
> > timestamp for the tag, not the post.
> 
> > Can someone help me fix this issue? Thank you!

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 3, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/named-scopes-and-created-at/180958/5 "2010-01-03T19:39:03Z")

</div>

On Jan 3, 5:14 pm, jm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Post Load (0.4ms) SELECT \* FROM “posts” INNER JOIN “posts\_tags” ON  
> “posts”.id = “posts\_tags”.post\_id WHERE (“posts\_tags”.tag\_id = 1 ) AND  
> ((“posts”.“published” = ‘t’) AND (“posts\_tags”.tag\_id = 1 )) ORDER BY  
> posts.created\_at DESC

This is probably the query at fault - you want to put the :select  
option on the association itself, not the named scope

Fred

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 4, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/named-scopes-and-created-at/180958/6 "2010-01-04T15:47:12Z")

</div>

Thank you so much! That worked perfectly! 🙂

On Jan 3, 12:37 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Fred  
> –

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
