我有一个模特:
def get_all_word_combinations(str, min_length)
chars = str.split('')
all_results = []
(min_length..str.size).each do |x|
chars.each_cons(x) do |r|
all_results << r.join
end
end
return all_results
end
和
class Entry < ActiveRecord::Base
belongs_to :organization
belongs_to :detail_organization, class_name: "Organization"
end
我能成功:
class Organization < ActiveRecord::Base
has_many :entries
end
但我一直在研究一个返回一个相当大的数据集的查询,该数据集被导出到需要拔除的xls。我试过了:
> entry = Entry.last.detail_organization
Entry Load (0.5ms) SELECT "entries".* FROM "entries" ORDER BY "entries"."id" DESC LIMIT $1 [["LIMIT", 1]]
Organization Load (0.3ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 69], ["LIMIT", 1]]
=> #<Organization:0x007fcabcabc
id: 69,
name: "Detail Office",
code: "DFO",
classification_id: 4,
active: true,
created_at: Thu, 05 Jan 2017 21:49:48 UTC +00:00,
updated_at: Thu, 05 Jan 2017 21:49:48 UTC +00:00>
有和没有包括相关组织表,但似乎无法与pluck建立联系。
有趣的是:
[111] pry(main)> @entries = Entry.scheduled_between(start_date, end_date).pluck('detail_organization.name')
(0.6ms) SELECT detail_organization.name FROM "entries" INNER JOIN "daily_logs" ON "daily_logs"."id" = "entries"."daily_log_id" WHERE ("daily_logs"."calendar_date" BETWEEN $1 AND $2) [["calendar_date", "01/06/2017"], ["calendar_date", "01/06/2017"]]
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "detail_organization"
LINE 1: SELECT detail_organization.name FROM "entries" INNER JOIN "d...
^
: SELECT detail_organization.name FROM "entries" INNER JOIN "daily_logs" ON "daily_logs"."id" = "entries"."daily_log_id" WHERE ("daily_logs"."calendar_date" BETWEEN $1 AND $2)
from /Users/atfreeme/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'
至少得到我的ids。
如果我希望对这个问题有足够的描述性,那么任何有任何建议或可能是新方向的人都会这样做?不确定这是PostgreSQL还是ActiveRecord。
答案 0 :(得分:2)
问题在于您的查询
ActiveRecord :: StatementInvalid:PG :: UndefinedTable:ERROR:缺少表“detail_organization”的FROM子句条目
ActiveRecord正在尝试搜索表detail_organization
,但您的数据库中没有该表
你应该用organizations
表查询detail_organization
只是一个关联名而不是一个表名
@entries = Entry.scheduled_between(start_date, end_date).joins(:detail_organization).pluck('organizations.name')