S
S
Splite2015-08-08 17:56:16
Ruby on Rails
Splite, 2015-08-08 17:56:16

How to make .includes work in RoR?

There are two models

class Version < ActiveRecord::Base
  has_many :servers
end

class Server < ActiveRecord::Base
  belongs_to :version
end

Those. each server has a version. I write, for example, such code Then I go through it in the view and try to display the version, if it exists (haml)
Server.includes(:version).all
- servers.each do |s|
    %p= s.version.name is s.version

But no version is displayed, although they definitely exist. Moreover, in the rails console , the Server.includes (:version).all code generates ONLY the SELECT "servers".* FROM "servers" query , although as far as I understand it should be the second one for the
UPD version query
. Just checked, this problem is only on postgresql. Two queries are generated on sqlite and everything works. At the same time, nothing else changed (I try to replace the development: block in databases.yml)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
thepry, 2015-08-08
@thepry

Perhaps your primary key indexes in the database are broken or something like that?

P
Pavel Kononenko, 2015-08-08
@premas

The query only looks like this if you don't have any version entries that have a server_id that matches servers.id. Check pg_admin3 for integrity of the database and for the presence of these fields and corresponding tables.
What is in the console after executing these commands?

Version.all.map &:server_id
Server.all.map &:version_ids

And after these?
Version.includes(:server).all.map &:server_id
Server.includes(:versions).all.map &:version_ids

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question