Home Select different table fields
Reply: 0

Select different table fields

user1109
1#
user1109 Published in June 23, 2018, 4:34 am

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"
end

class Recipe < ApplicationRecord
  belongs_to :category
end

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?

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO