RailsCasts Pro episodes are now free!

Learn more or hide this

TopperH's Profile

GitHub User: TopperH

Comments by

Avatar

Great screenscast.

I'm trying to adapt it to a blog app where the posts have a "related posts" feature.

So one post has many related_posts trough related_posts_association.

While the implementation works out of the box and I can create relationships in the console I can't find a way to make it work in the application.

In this railscast example the create action uses the "current_user" that is stored in a session by the auth system. In my example I would like to create the relationship in the _form view (new and edit) where I create a list of all the other posts, each one next to a checkbox.

Once the create or update button is pressed I would like to have also the relationships created.

Does anyone have an idea on how to implement that?

Avatar

Well, both of those return an empty array. I guess this is because it is looking for tags that match both conditions (which is impossible since the name attribute is unique), instead of looking for posts that have both the tags.

Anyway thank you for pointing me to arel_table, I'll have a deeper look on that to see if it is what I need.

Avatar

@Patrick, that actually gives me the same result.

Let me explain with better words what I'm trying to achieve:

post1 has tags "foo, bar, baz"
post2 has tags "foo, bar"
post3 has tags "foo, baz"

I need to a query that passing "foo, baz" returns only post1 and post3, since they are the only posts to have both foo and baz.

Thank you for your help.

Avatar

What if I want to query for articles that belong to multiple tags?

I tried this method:

ruby
Article.joins(:tags).where(:tags => { :name => ["foo", "bar"] } )

but this returns me every post that has either tag foo, or bar or both.

How do I search only for posts that have both foo and bar at the same time?