I’m new in rails and I’m changing a backend from laravel to rails. My problem is with the inclusion of the data from other model.
I have two models: Questions and User who created the Question. I’ve included the models, controller, and migration of Question and the images about the the bug and the response without the relation expected. The situation is : even establishing the relationships when I use includes the user data is not found by the query even though a query to be correct when a I copy and execute in postgres terminal.
class User < ApplicationRecord
has_secure_password
validates :name, presence: true
validates :email, presence: true, uniqueness: { case_sensitive: false }, format: { with: /\A([\w+\-]\.?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/, message: I18n.t("errors.models.user.format_email") }
validates :password, format: { with: /\A(?=.*).{8,72}\z/, message: I18n.t("errors.models.user.format_password") }, if: :password_required?
before_save :downcase_email!
after_create :send_confirm_email
has_many :sessions, dependent: :destroy
has_many :user_verifications, dependent: :destroy
has_many :courses
has_many :questions
class Question < ApplicationRecord
has_many :replies
belongs_to :user
validates :title, presence: true
validates :category, presence: true
validates :body, presence: true
validates :tags, presence: true
default_scope {includes (:user)}
scope :with_replies, ->{ includes(:replies)}
end
class CreateQuestions < ActiveRecord::Migration[6.1]
def change
create_table :questions do |t|
t.references :user, null: false, foreign_key:true, index: true
t.string :title
t.string :category
t.text :body
t.boolean :solved, default: false
t.string :tags, array:true, default: []
t.integer :replies_count, default:0
t.timestamps
end
end
end