Rails: self join scheme with has_and_belongs_to_many?

Here are some resources which may be helpful: RailsCasts episode #163 Self-Referential Association regarding self-referential many-to-many relationships RailsCasts episode #47 Two Many-to-Many. This might be more relevant to what you’re attempting to accomplish A gist someone created for self-referential relationships using HABTM I’ll summarize the information found in those links: Given that you’re describing a … Read more

Explanation of self-joins

You can view self-join as two identical tables. But in normalization, you cannot create two copies of the table so you just simulate having two tables with self-join. Suppose you have two tables: Table emp1 Id Name Boss_id 1 ABC 3 2 DEF 1 3 XYZ 2 Table emp2 Id Name Boss_id 1 ABC 3 … Read more

How to get matching data from another SQL table for two different columns: Inner Join and/or Union?

(The following applies when every row is SQL DISTINCT, and outside SQL code similarly treats NULL like just another value.) Every base table has a statement template, aka predicate, parameterized by column names, by which we put a row in or leave it out. We can use a (standard predicate logic) shorthand for the predicate … Read more