Rails double match from has_and_belongs_to_many - sql

Say that I have a has_and_belongs_to_many relationship where I have posts and categories. It is simple to find all the posts in a category, or all the categories that a particular post is a member of. However, what if I want to find a list of posts that belong to multiple categories? For example, a list of posts that are on the topic of security in Rails, I might want the posts that belong to the categories "Security" and "Rails".
Is it possible to do this with the finder methods build into ActiveRecord, or will I need to use SQL? Can someone please explain how?

You can use includes or joins, like:
#result = Post.includes(:categories).where("categories.name = 'Security' OR categories.name = 'Rails'")
or
#result = Post.joins(:categories).where("categories.name = 'Security' OR categories.name = 'Rails'")
I also suggest to check this railscast to understand the difference between joins and includes, so you can decide what is better in your case.

i don't know anything about rails, but i'm attempting a similar thing with some sql. this may or may not work for either of us....
i have a table of articles, and a look-up table of applied categories. to get an article that has the 'security' category and the 'rails' category, i'm joining the article table to the category table, of course, but also re-joining it a second time. each join of the category table uses a hint in the table alias name (ie language or topic)
pseudo code:
SELECT article.*,
category_language.category_id,
category_topic.category_id
FROM category category_language
INNER JOIN article ON category_language.articleID = article.articleID
INNER JOIN category category_topic ON article.articleID = category_topic.articleID
WHERE category_language.category_id in (420) /* rails */
and category_topic.category_id in (421) /* security */
this isn't completely ironed out, and i hope that if i am showing my ignorance here, someone will speak up.

Related

What am I / Was I missing on this SQL request?

I apologize for any vagueness, this is about a question I had on a entry level SQL position test last week. I couldn't figure out how to do this at the time, and can't seem to figure anything out now.
Basically I was provided with 3 tables. One was Recipes (had recipe name, ID, instructions and notes), one was RecipeIngredients (had recipe ID, ingredient ID, ingredients, and quantity of ingredients), and the third was Ingredients (ingredient ID and ingredient). Something like that.
I had a few queries with JOIN statements that showed how to make certain recipes and so on. But I couldn't figure out quite how to manage the final question. The final question was something like:
"Provide 2 sets of queries at once. 1st query - Return Ingredients, quantities, and notes for a specific ID. 2nd Query - Return the instructions for the same Recipe ID. Write the queries so that the user can easily alter the recipe ID in one place only for both queries in order to query for different recipe IDs."
I know we can't alias a WHERE clause, but that is the only thing I can remotely think of for doing 2 queries at once with only specifying the WHERE once. I tried to see if I could do it with a subquery but had no luck. I considered UNION... but there were different columns and different values in each query so that's a no go.
Is there something I'm missing? Or did I just completely fail when trying to set this up as a subquery? I apologize for vagueness, it's been a few days and I've been too busy to remember to post this on here. I've found a lot of help anonymously browsing this site in the past so I figured it was worth posting this as I've not seen anything similar so far.
Using SQL Server, you could have declared a variable, like #recipeID, then used it in two queries. That would allow you to change the value in one place, and have it used in 2 queries.
DECLARE #recipeID INT = 123
SELECT *
FROM recipes
WHERE recipeID = #recipeID
SELECT *
FROM recipe_ingredients
WHERE recipeID = #recipeID

Many to many relation, multi where clause on the same column and hibernate

Sorry for the bad question title, couldnt think of anything better.
Anyway, my tables are Tags - Poststags - Posts. Poststags is a junction table for many to many relation. I need to select all posts with given tags, I dont know how many tags the user will choose to search for. One way I found to do this is in the code below, however i would need to loop all the tags given by the user and construct the query string from there since the number of tags is unknown. Seems like a pretty bad solution to me.
Another solution would be to store all tags in one column in the Posts table as a pure string, but I dont want to do that because of other application requirements.
I have a working sql query, since I was trying pure sql before trying to implement it in hibernate, but I dont like doing a select of all posts containing each tag and then joining each query, is there a way to specify the same column multiple times in the WHERE clause? Something along the lines WHERE pt.tag_id = x AND pt.tag_id = y? (I know this won't work). IN operator won't work either since it will give me Posts that contain any of the supplied tags and not just the posts containing ALL of the supplied tags.
Also how would I implement such a query in HQL(if subqueries like this are even supported?). Or can I somehow manage this via criteria? Or do I have to resort to using createSQLQuery method of a hibernate session?
SELECT * FROM
( SELECT * FROM posts p
inner join poststags pt on pt.post_id = p.id
WHERE pt.tag_id = 1 ) AS A
INNER JOIN
( SELECT * FROM posts p
inner join poststags pt on pt.post_id = p.id
WHERE pt.tag_id = 2 ) AS B ON A.id = B.id
And yes, I know this query is not returning the Post entity itself, but I can handle that later.
Don't use hibernate or ORM for this kind of complex select, it may work, but in a bad way.
Your use case should be solved by full text search, which means each Post will need have its own tags.
I don't see much value to make Tag an entity. It's just a string.
Full text search could be heavy for database , A better way is using elasticsearch to help. Spring has integration with spring-data-elasticsearch and it's not difficult to use. Elasyicsearch is very powerful for free text search.
Here is a solution that 'should' work using Criteria queries in Hibernate.
Assuming that you have an entity for Post and an entity for PostTag and PostTag has reference to Post (which I think it should given the example query that you provided), I believe that something like this should do what you want:
static DetachedCriteria getPostTagCriteria(String tagString)
{
DetachedCriteria criteria = DetachedCriteria.forClass(PostTag.class, "uniqueName_" + postTagId);
criteria.createAlias("tag", "tag");
criteria.add(Restrictions.eq("tag.tagString", tagString));
criteria.setProjection(Projections.property("postId"));
return criteria;
}
static List<Post> getPosts(List<String> tagStrings)
{
Criteria criteria = getCurrentSession().createCriteria(Post.class, "post");
for(String tagString : tagStrings)
{
criteria.add(Property.forName("post.id").in(getPostTagCriteria(tagString)));
}
List<Post> ret = criteria.list();
return ret;
}
This assumes that you have reasonable entities to represent Post, PostTag and Tag and that they all reference each other in obvious parent/child sort of ways that I have completely made up here.
But, the general idea of creating multiple detached criteria objects based on your input should solve your problem. This solution also comes with the same caveats regarding SQL complexity mentioned above. You will be creating a sub-query for each tag passed in. So, depending on your indexes and table sizes, you may need to consider a different approach.

Rails ActiveRecord finding questions by tag in named scope

I want the equivalent of SO search by tag, so I need an exists query but I also still need to left join on all tags. I've tried a couple of approaches and I'm out of ideas.
The Qustion - Tag relationship is through has_and_belongs_to_many both ways (i.e. I have a QuestionTags joiner table)
e.g.
Question.join(:tags).where('tag.name = ?', tag_name).includes(:tags)
I would expect this to do what I need but actually it just mashes up the includes with the join and I just end up with basically an inner join.
Question.includes(:tags)
.where("exists (
select 1 from questions_tags
where question_id = questions.id
and tag_id = (select id
from tags
where tags.name = ?))", tag_name)
This fetches the correct results but a) is really ugly and b) gives a deprecation warning as again it seems to confuse the includes with the join:
DEPRECATION WARNING: It looks like you are eager loading table(s) (one
of: questions, tags) that are referenced in a string SQL sn ippet. For
example:
Post.includes(:comments).where("comments.title = 'foo'")
Note I'm trying to write these as named scopes.
Let me know if the question isn't clear. Thanks in advance.
OK, got it. I know no built in syntax to do it. I have used an alternative before, You can do like this:
Question.include(:tags).where("questions.id IN (
#{ Question.joins(:tags).where('tags.name = ?', tag_name).select('questions.id').to_sql})")
You can also join this subquery to your questions table instead of using IN. Alternatively if You are not against adding gems and You are using Postgres, use this gem.
It provides really neat syntax for advanced queries.
Use preload instead of includes:
Question.preload(:tags).where("exists ....

Master-detail Filtering in SQL

I have an interesting query that I would like to get done:
1. I have an Article table [ArticleId, ArticleName]
2. An Article has an ArticleCheckin (1:1) [ArticleId, CheckinName]
3. An Article can have multiple ArticleResources [ArticleResourceId, ArticleId, ResourceName]
I would like to write a query that gets me a list of Articles with Checkin (if exists) and a list of the Article Resources for each Article in the same query.
Currently, I obtain the Article and am querying the Resources again for each article and that doesn't seem like the right way to do this.
Any help would be great!
AFAIK a query will always give you a flattened out result. This means that in order to get a list of Articles and lists of corresponding ArticleResources, you'll need to loop through each Article like you are doing now.
But you are not saying anything about which technology you are using, so it is hard to suggest how you can improve on your query/code.
This query will give you all articles with or without checkins but only if there are article resources (LEFT JOIN otherwise).
SELECT A.*, AC.*, AR.*
FROM Article A
LEFT JOIN ArticelCheckin AC ON AC.ArticleId = A.ArticleId
INNER JOIN ArticleResources AR ON AR.ArticleId = A.ArticleId

MySQL: Limit output according to associated ID

So here's my situation. I have a books table and authors table. An author can have many books... In my authors page view, the user (logged in) can click an author in a tabled row and be directed to a page displaying the author's books (collected like this URI format: viewauthorbooks.php?author_id=23), very straight forward... However, in my query, I need to display the books for the author only, and not all books stored in the books table (as i currently have!) As I am a complete novice, I used the most simple query of:
SELECT * FROM books
This returns the books for me, but returns every single value (book) in the database, and not ones associated with the selected author. And when I click a different author the same books are displayed for them...I think everyone gets what I'm trying to achieve, I just don't know how to perform the query. I'm guessing that I need to start using more advanced query clauses like INNER JOIN etc. Anyone care to help me out :)
Enters the WHERE clause:
The WHERE clause is used to extract only those records that fulfill a specific criteria. In your case, all you need to do is:
SELECT * FROM tasks_tb WHERE author_id = '23';
You will obviously need to change the '23' with the value passed in the URL querystring, so that each page lists the books of each relevant author.
Since it is never too early to start reading about best practices, note that for public websites it is really dangerous to include any un-sanitized input into an SQL query. You may want to read further on this topic from the following Stack Overflow posts:
XKCD sql injection - please explain (with pictures!)
What is SQL injection?
Is SQL injection a risk today?
SQL Injection Topics on Stack Overflow
The basic syntax is
SELECT * FROM table WHERE column=value;
But you really need to study more SQL, I'd suggest going through sqlzoo tutorial. http://sqlzoo.net/