Home Select different table fields
Reply: 0

Select different table fields

user4418 Published in September 20, 2018, 12:45 pm

I use rails 4 and mysql. I have two Recipe and Category models.

class Category < ApplicationRecord
  has_many :recipes
  validates_uniqueness_of :name, message: "deve ser ├║nico"
  validates_presence_of :name, message: "can't be blank"

class Recipe < ApplicationRecord
  belongs_to :category

The Recipe fields are: id, title, resume, and category_id which is the reference for category.

Category fields are: id and name

I'm trying to make a query that through the Recipe table I get as a result all the fields of the two tables. I am trying to do this with the following query:

Recipe.joins("INNER JOIN categories ON categories.id = recipes.category_id").includes("category")

But the result gives me results only from the Recipe table. The result is:

Recipe id: 1, title: "ababa", resume: nil, created_at: "2018-02-07 23:16:19", updated_at: "2018-02-07 23:16:19", category_id: 9

How can I make all the data in the two tables appear?

share|improve this question

3 Answers 3

active oldest votes
up vote 2 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 1.194814 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO