- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这非常令人沮丧。我试图通过工作获得 has_many,我想我离这个太近了,看不到一些非常明显的东西。每个步骤都可以正常工作,Rails 生成的 SQL 也可以正常工作,但在控制台中却不能。
关于整个设置的一件奇怪的事情是在 salesforce 架构中有几个表,并且表名和主键不是标准的。这是基本结构:
class Contact
self.table_name = 'salesforce.contact'
self.primary_key = 'sfid'
has_many :content_accesses
has_many :inventories, through: :content_accesses # I've tried inventory and inventorys, just to ensure it's not Rails magic
end
class ContentAccess
belongs_to :inventory
belongs_to :contact
end
class Inventory
self.table_name = 'salesforce.inventory__c'
self.primary_key = 'sfid'
has_many :content_accesses, foreign_key: 'inventory_id'
end
作品:
c = Contact.first
c.content_accesses # works, gives the related items
c.content_accesses.first.inventory # works, gives the related Inventory item
错误:
c.inventories # Gives:
# ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "content_accesses" does not exist)
# LINE 1: ..._c".* FROM "salesforce"."inventory__c" INNER JOIN "content_a...
# ^
# : SELECT "salesforce"."inventory__c".* FROM "salesforce"."inventory__c" INNER JOIN "content_accesses" ON "salesforce"."inventory__c"."sfid" = "content_accesses"."inventory_id" WHERE "content_accesses"."contact_id" = $1 LIMIT $2
当我通过 Postico 运行该查询时, 不过,它工作正常。 🤬
编辑添加:
表的 DDL:
CREATE TABLE salesforce.inventory__c (
createddate timestamp without time zone,
isdeleted boolean,
name character varying(80),
systemmodstamp timestamp without time zone,
inventory_unique_name__c character varying(255),
sfid character varying(18),
id integer DEFAULT nextval('salesforce.inventory__c_id_seq'::regclass) PRIMARY KEY,
_hc_lastop character varying(32),
_hc_err text
);
CREATE UNIQUE INDEX inventory__c_pkey ON salesforce.inventory__c(id int4_ops);
CREATE INDEX hc_idx_inventory__c_systemmodstamp ON salesforce.inventory__c(systemmodstamp timestamp_ops);
CREATE UNIQUE INDEX hcu_idx_inventory__c_sfid ON salesforce.inventory__c(sfid text_ops);
CREATE TABLE salesforce.contact (
lastname character varying(80),
mailingpostalcode character varying(20),
accountid character varying(18),
assistantname character varying(40),
name character varying(121),
mobilephone character varying(40),
birthdate date,
phone character varying(40),
mailingstreet character varying(255),
isdeleted boolean,
assistantphone character varying(40),
systemmodstamp timestamp without time zone,
mailingstatecode character varying(10),
createddate timestamp without time zone,
mailingcity character varying(40),
salutation character varying(40),
title character varying(128),
mailingcountrycode character varying(10),
firstname character varying(40),
email character varying(80),
sfid character varying(18),
id integer DEFAULT nextval('salesforce.contact_id_seq'::regclass) PRIMARY KEY,
_hc_lastop character varying(32),
_hc_err text
);
CREATE UNIQUE INDEX contact_pkey ON salesforce.contact(id int4_ops);
CREATE INDEX hc_idx_contact_systemmodstamp ON salesforce.contact(systemmodstamp timestamp_ops);
CREATE UNIQUE INDEX hcu_idx_contact_sfid ON salesforce.contact(sfid text_ops);
CREATE TABLE content_accesses (
id BIGSERIAL PRIMARY KEY,
inventory_id character varying(20),
contact_id character varying(20),
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
CREATE UNIQUE INDEX content_accesses_pkey ON content_accesses(id int8_ops);
编辑 2:作为调试的一部分,我尝试在控制台中运行生成的查询:
ActiveRecord::Base.connection.execute
运行生成的查询,则查询有效。Contact.connection.execute
运行它,它会给出同样的错误。 感觉 Rails 没有搞清楚什么,但我想不通在哪里、为什么或什么。
编辑 3: 根据要求,框架跟踪:
activerecord (5.2.0) lib/active_record/connection_adapters/postgresql_adapter.rb:669:in `prepare'
activerecord (5.2.0) lib/active_record/connection_adapters/postgresql_adapter.rb:669:in `block in prepare_statement'
/Users/timsullivan/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
activerecord (5.2.0) lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `prepare_statement'
activerecord (5.2.0) lib/active_record/connection_adapters/postgresql_adapter.rb:609:in `exec_cache'
activerecord (5.2.0) lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `execute_and_clear'
activerecord (5.2.0) lib/active_record/connection_adapters/postgresql/database_statements.rb:81:in `exec_query'
activerecord (5.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:469:in `select_prepared'
activerecord (5.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:55:in `select_all'
activerecord (5.2.0) lib/active_record/connection_adapters/abstract/query_cache.rb:101:in `select_all'
activerecord (5.2.0) lib/active_record/querying.rb:41:in `find_by_sql'
activerecord (5.2.0) lib/active_record/relation.rb:554:in `block in exec_queries'
activerecord (5.2.0) lib/active_record/relation.rb:578:in `skip_query_cache_if_necessary'
activerecord (5.2.0) lib/active_record/relation.rb:542:in `exec_queries'
activerecord (5.2.0) lib/active_record/association_relation.rb:34:in `exec_queries'
activerecord (5.2.0) lib/active_record/relation.rb:414:in `load'
activerecord (5.2.0) lib/active_record/relation.rb:200:in `records'
activerecord (5.2.0) lib/active_record/relation.rb:195:in `to_ary'
activerecord (5.2.0) lib/active_record/relation/finder_methods.rb:530:in `find_nth_with_limit'
activerecord (5.2.0) lib/active_record/associations/collection_proxy.rb:1136:in `find_nth_with_limit'
activerecord (5.2.0) lib/active_record/relation/finder_methods.rb:515:in `find_nth'
activerecord (5.2.0) lib/active_record/relation/finder_methods.rb:125:in `first'
actionview (5.2.0) lib/action_view/template.rb:159:in `block in render'
activesupport (5.2.0) lib/active_support/notifications.rb:170:in `instrument'
actionview (5.2.0) lib/action_view/template.rb:354:in `instrument_render_template'
actionview (5.2.0) lib/action_view/template.rb:157:in `render'
actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (5.2.0) lib/action_view/renderer/abstract_renderer.rb:44:in `block in instrument'
activesupport (5.2.0) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.0) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.0) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.2.0) lib/action_view/renderer/abstract_renderer.rb:43:in `instrument'
actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (5.2.0) lib/action_view/renderer/template_renderer.rb:16:in `render'
actionview (5.2.0) lib/action_view/renderer/renderer.rb:44:in `render_template'
actionview (5.2.0) lib/action_view/renderer/renderer.rb:25:in `render'
actionview (5.2.0) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.2.0) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionview (5.2.0) lib/action_view/rendering.rb:84:in `render_to_body'
actionpack (5.2.0) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.2.0) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (5.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (5.2.0) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
activesupport (5.2.0) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/Users/timsullivan/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
activesupport (5.2.0) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.2.0) lib/active_record/railties/controller_runtime.rb:31:in `cleanup_view_runtime'
actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:45:in `render'
actionpack (5.2.0) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (5.2.0) lib/abstract_controller/base.rb:194:in `process_action'
actionpack (5.2.0) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.2.0) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (5.2.0) lib/active_support/callbacks.rb:132:in `run_callbacks'
actionpack (5.2.0) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (5.2.0) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (5.2.0) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.0) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.0) lib/active_support/notifications.rb:168:in `instrument'
actionpack (5.2.0) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (5.2.0) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
activerecord (5.2.0) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
actionpack (5.2.0) lib/abstract_controller/base.rb:134:in `process'
actionview (5.2.0) lib/action_view/rendering.rb:32:in `process'
actionpack (5.2.0) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (5.2.0) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (5.2.0) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
actionpack (5.2.0) lib/action_dispatch/routing/route_set.rb:34:in `serve'
actionpack (5.2.0) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.0) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.0) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.0) lib/action_dispatch/routing/route_set.rb:840:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.5) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.0.5) lib/rack/etag.rb:25:in `call'
rack (2.0.5) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.5) lib/rack/head.rb:12:in `call'
actionpack (5.2.0) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.5) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.5) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/cookies.rb:670:in `call'
activerecord (5.2.0) lib/active_record/migration.rb:559:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.0) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.0) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
airbrake (7.2.1) lib/airbrake/rack/middleware.rb:52:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
web-console (3.6.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.6.1) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.6.1) lib/web_console/middleware.rb:20:in `catch'
web-console (3.6.1) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.0) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.0) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.5) lib/rack/method_override.rb:22:in `call'
rack (2.0.5) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
webpacker (3.4.3) lib/webpacker/dev_server_proxy.rb:18:in `perform_request'
rack-proxy (0.6.4) lib/rack/proxy.rb:57:in `call'
railties (5.2.0) lib/rails/engine.rb:524:in `call'
puma (3.11.4) lib/puma/configuration.rb:225:in `call'
puma (3.11.4) lib/puma/server.rb:632:in `handle_request'
puma (3.11.4) lib/puma/server.rb:446:in `process_client'
puma (3.11.4) lib/puma/server.rb:306:in `block in run'
puma (3.11.4) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
最佳答案
既然你说,当你直接调用生成的 SQL 时,问题的根源在于将返回的数据映射回对象的过程中的某个地方。尽管您的设置看起来不错,但看起来很不标准,所以我会尝试为 Rails 提供更多关于关联如何归属的提示。
首先,您应该为您的through
关系设置一个source
(docs) :
has_many :inventories, through: :content_accesses, source: :inventory
如果仍然不能给 Rails 提供正确的线索,您可以尝试设置 inverse_of
、foreign_key
、primary_key
甚至 class_name
在其他 belongs_to
和 has_many
关联上,为 Rails 提供所需的提示。很难说什么可能有帮助,但在非标准设置中,您有时会遇到自动推断名称的某些问题。
关于ruby-on-rails - ActiveRecord::StatementInvalid、PG::UndefinedTable 错误,但生成的 SQL 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51829428/
我想知道如何使用 CaSTLe AR 来“播种”自动递增的主键值?例如,希望 Orders 表的主键从 10000 开始。这是 1. 可能的 2. 创建订单号的好解决方案吗? 也许有一种方法可以在不是
假设你想 records = Model.all records.to_a.map{|m| m.serializable_hash(:root => true)} 就像 to_json(:root =
我目前正在尝试开发一个插件/gem 来观察多个模型。理想情况下,观察者应该只用一个单例方法自动实例化...... class MyModel true observe ActiveRecord:
使用 Mysql 我可以使用 COALESCE仅更新表中为空的值。 我怎样才能用 Rails (ActiveRecord) 做到这一点? 我不想创建 if表中每一列的语句,我猜如果我将 ActiveR
我从 Yii2 开始,想要将一些内容保存到我的数据库中。这是我的模型: class Course extends ActiveRecord { public $name; public
我正在开始一个新项目,最近我发现了 caSTLe 项目 activerecord,这似乎是一个很棒的解决方案,但与此同时,它看起来确实是非常规的。我想知道,这种感觉是来自学习新东西(我应该习惯它)还是
不知何故,我总是在周五收到这些。 我之前的问题是关于同样的问题,但我现在可以缩小范围: 我一整天都在研究这个问题,试图理解它。我有一个带有 lock_version 列的表,如此指定: add_col
我正在使用表中缓存的关键字构建搜索。在表中查找用户输入的关键字之前,它会被规范化。例如,删除了一些标点符号,如“-”,并标准化了大小写。然后使用规范化的关键字来查找获取搜索结果。 我目前正在使用 be
我有以下类用于“项目”和“颜色”之间的多对多关系。 并且“项目”不应该有重复的“颜色”,例如:-如果“Item1”有“Blue”和“Red”,那么我们不能向“Item1”添加另一个“Red” 这是正确
我对 Yii2 事件记录处理关系属性的方式感到困惑。是否可以将事件记录对象链接到另一个对象而不先保存它? 例如,我想将图像作为 Logo 添加到公司记录中,但尚未决定是否应保存这两个记录。给定一家公司
我决定使用 Castle ActiveRecord 为客户制作一个系统,一切都很顺利,直到我发现交易不起作用,例如; TransactionScope t = new T
Yii2 的新特性。 只是试图从 ActiveRecord 查询中获得返回。我意识到使用 Yii2 约定可能有更简单的方法来做到这一点 public function actionGet_permis
我收到一个错误(在 Sinatra + ActiveRecord Heroku 上)数据库配置没有指定适配器。 根据一些研究,这似乎是因为 Heroku 预计在 rackup 期间不使用环境变量。 我
我正在尝试按“created_at”日期查找记录 - 数据库列类型为“日期时间”并且 我正在使用来自 jQuery 的 UI DatePicker 我的网址如下所示:“localhost:3000/u
我想问这个问题。但我认为 BlameableBehavior和 TimestampBevavior可能是需要这样做的好例子: 我必须做什么才能使属性只能从 ActiveRecord 对象的外部读取而不
这是一个古老的问题,其中给定一个具有“类型”、“品种”和“价格”属性的表,您可以获取每种类型的最低价格的记录。 在 SQL 中,我们可以做 this通过: select f.type, f.varie
这似乎是一个微不足道的问题,但是我能想到的所有明显的解决方案都有自己的缺陷。 我们想要的是能够为新记录设置任何默认的 ActiveRecord 属性值,以使其在验证之前和期间可读并且不干扰用于搜索的派
做关联方法,比如has_many定义的那些和 belongs_to利用 ActiveRecord::Relation ? 如果是这样,是否有可能获得 ActiveRecord::Relation正在使
如何将 MySQL 的 WHERE 转换为 ActiveRecord :condition? SELECT * FROM users WHERE LENGTH(users.last_name) [
当使用带有 ActiveRecord T4 模板的 SubSonic 3 时,生成的代码会显示许多关于 CLS 合规性、未使用的项目和缺少 GetHashCode() 实现的警告。 为了避免它们,我做
我是一名优秀的程序员,十分优秀!