PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How do I include a has_many relation when doing a Rails entity query?

 October 21, 2022     has-many, relation, ruby-on-rails-5     No comments   

Issue

I'm using Rails 5. I have a model with a has_many relation ...

class Book

    has_many :references, :autosave => true, :dependent => :destroy

When querying for the entity,

book = Book.find(params[:id])

what do I need to add so that the has_many relation entities are also included?


Solution

You can use includes method for eager loading:

Book.includes(:references).find(params[:id])


Answered By - demir
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing