Query records through its belongs_to relation in Rails

With the latest rails versions you can do: Activity.joins(:location).where(locations: { country: “Australia” }) Beware: it is location (singular) in joins(:location) because it references the belongs_to relationship name it is locations (plural) in where(…) because it references the table name The latter means that if you had the following: belongs_to :location, class_name: “PublicLocation” the query would … Read more

How to find all the relations between all mysql tables?

The better way, programmatically speaking, is gathering data from INFORMATION_SCHEMA.KEY_COLUMN_USAGE table as follows: SELECT `TABLE_SCHEMA`, — Foreign key schema `TABLE_NAME`, — Foreign key table `COLUMN_NAME`, — Foreign key column `REFERENCED_TABLE_SCHEMA`, — Origin key schema `REFERENCED_TABLE_NAME`, — Origin key table `REFERENCED_COLUMN_NAME` — Origin key column FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` — Will fail if user don’t have privilege WHERE … Read more

utf8_bin vs. utf_unicode_ci

It depends on what you need. The utf8_bin collation compares strings based purely on their Unicode code point values. If all of the code points have the same values, then the strings are equal. However, this falls apart when you have strings with different composition for combining marks (composed vs. decomposed) or characters that are … Read more

SQL ON DELETE CASCADE, Which Way Does the Deletion Occur?

Cascade will work when you delete something on table Courses. Any record on table BookCourses that has reference to table Courses will be deleted automatically. But when you try to delete on table BookCourses only the table itself is affected and not on the Courses follow-up question: why do you have CourseID on table Category? … Read more