Performs a joins on args:
User.joins(:posts) => SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id"
You can use strings in order to customize your joins:
# File activerecord/lib/active_record/relation/query_methods.rb, line 428 def joins(*args) check_if_method_has_arguments!(:joins, args) spawn.joins!(*args) end
I came across the following situation An article has a history of friendly url being that the foreign key that represents the value of the article’s id in the table is called Friend url_id then in that case:
Article.joins(“INNER JOIN friends ON articles.id = friends.url_id”).where(“friends.url like ? ”, url)
if the column url_id was renamed for artigo_id would be easier
Article.joins(:friend).where(“friends.url like ? ”, url)