- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
创建连接表时如下
class Project < ActiveRecord::Base
belongs_to :user
has_many :time_pledges, dependent: :destroy
has_many :volunteers, through: :time_pledges
class User < ActiveRecord::Base
has_many :projects, dependent: :destroy
has_many :time_pledges, foreign_key: "volunteer_id", dependent: :destroy
has_many :volunteering, through: :time_pledges, source: :project
class TimePledge < ActiveRecord::Base
belongs_to :volunteer, class_name: "User"
belongs_to :project
我能够执行以下操作 @project.volunteers.count
并得到当时有多少用户志愿参与特定项目的答案。但是,当我更新项目模型以便能够访问连接表模型 (time_pledges
) 的 hours_pledged 属性时,如下所示:
has_many :volunteers, -> { select('users.*, time_pledges.hours_pledged as hours_pledged')}, through: :time_pledges
我无法再访问@project.volunteers.count.
我得到的错误如下
P1.volunteers
User Load (1.6ms) SELECT users.*, time_pledges.hours_pledged as hours_pledged FROM "users" INNER JOIN "time_pledges" ON "users"."id" = "time_pledges"."volunteer_id" WHERE "time_pledges"."project_id" = $1 [["project_id", 300]]
=> #<ActiveRecord::Associations::CollectionProxy [#<User id: 1, email: "durham@example.com", encrypted_password: "$2a$10$4fyCd4GGtwZ0NRzrJuPDd.KWAhXWWumJ1LqtqZOSYWQ...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-03-06 08:59:06", updated_at: "2015-03-06 08:59:06">]>
>> P1.volunteers.count
PG::SyntaxError: ERROR: syntax error at or near "as"
LINE 1: SELECT COUNT(users.*, time_pledges.hours_pledged as hours_pl...
^
: SELECT COUNT(users.*, time_pledges.hours_pledged as hours_pledged) FROM "users" INNER JOIN "time_pledges" ON "users"."id" = "time_pledges"."volunteer_id" WHERE "time_pledges"."project_id" = $1
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "as"
LINE 1: SELECT COUNT(users.*, time_pledges.hours_pledged as hours_pl...
^
: SELECT COUNT(users.*, time_pledges.hours_pledged as hours_pledged) FROM "users" INNER JOIN "time_pledges" ON "users"."id" = "time_pledges"."volunteer_id" WHERE "time_pledges"."project_id" = $1
此外,在我尝试执行 @project.volunteers.count
之后,我无法再访问我的任何用户(直到我重新启动控制台 session )。例如,如果我在 @project.volunteers.count
之后执行类似 U5=User.find_by(id:5)
的操作,我会收到以下消息:
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 5]]
PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
: SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
: SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1
感谢任何帮助谢谢
编辑 1
架构
create_table "time_pledges", force: :cascade do |t|
t.integer "volunteer_id"
t.integer "project_id"
t.integer "hours_pledged"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "time_pledges", ["project_id"], name: "index_time_pledges_on_project_id", using: :btree
add_index "time_pledges", ["volunteer_id", "project_id"], name: "index_time_pledges_on_volunteer_id_and_project_id", unique: true, using: :btree
add_index "time_pledges", ["volunteer_id"], name: "index_time_pledges_on_volunteer_id", using: :btree
使用 times_pledges 而不是 time_pledges
>> P1=Project.first Project Load (1.7ms) SELECT "projects".* FROM "projects" ORDER BY "projects"."created_at" DESC LIMIT 1 => #<Project id: 301, title: "awe", user_id: 101, created_at: "2015-03-06 15:19:17", updated_at: "2015-03-06 15:19:17", required_hours: 7> >> P1.volunteers NameError: uninitialized constant Project::TimesPledge
使用
has_many :volunteers, -> { select('users.*, time_pledges.hours_pledged hours_pledged')}, through: :time_pledges
给予
>> P1.volunteers
User Load (1.4ms) SELECT users.*, time_pledges.hours_pledged hours_pledged FROM "users" INNER JOIN "time_pledges" ON "users"."id" = "time_pledges"."volunteer_id" WHERE "time_pledges"."project_id" = $1 [["project_id", 301]]
=> #<ActiveRecord::Associations::CollectionProxy []>
>> P1.volunteers.count
PG::SyntaxError: ERROR: syntax error at or near "hours_pledged"
LINE 1: SELECT COUNT(users.*, time_pledges.hours_pledged hours_pledg...
^
: SELECT COUNT(users.*, time_pledges.hours_pledged hours_pledged) FROM "users" INNER JOIN "time_pledges" ON "users"."id" = "time_pledges"."volunteer_id" WHERE "time_pledges"."project_id" = $1
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "hours_pledged"
LINE 1: SELECT COUNT(users.*, time_pledges.hours_pledged hours_pledg...
^
: SELECT COUNT(users.*, time_pledges.hours_pledged hours_pledged) FROM "users" INNER JOIN "time_pledges" ON "users"."id" = "time_pledges"."volunteer_id" WHERE "time_pledges"."project_id" = $1
最佳答案
在使用 Postgresql 时,我不使用 AS,除非它用于我要加入的选择语句,即
SELECT parts.*, alias_skus
FROM parts
LEFT JOIN (
SELECT part_id, array_agg(sku) alias_skus
FROM part_aliases GROUP BY part_id)
AS sub1
ON parts.id = sub1.part_id;
对于列别名,只需省略“AS”。你可以在这里看到我正在使用别名 alias_skus
。
因此:
has_many :volunteers, -> { select('users.*, time_pledges.hours_pledged hours_pledged')}, through: :time_pledges
关于sql - 将选择添加到模型会删除 .count 功能(导轨),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28899363/
我有以下创建并执行 SQL 查询的方法: def edition_authors(edition, authors) query_string = "contributor_type = ? AN
使用 axlsx可以添加边框。我添加了这样的东西 red_border = p.workbook.styles.add_style :border => { :style => :thin, :col
我正在构建一个应用程序,该应用程序必须向多个雇主分配任务。 我已经建立了这些模型: #assignment.rb class Assignment :employer_assignments end
我正在尝试从 rails 2.3.x(使用 subdomain_routes 插件)转换一些子域路由,如下所示: map.subdomain :biz do |biz| biz.resources
到目前为止,这就是我所做的。 我在 Capfile 中加载了资源:js 仍然无法正常工作 http://guides.rubyonrails.org/asset_pipeline.html#preco
我正在使用最新的 ruby+rails,以及 filterrific gem。它工作得很好 - 但如何在每个范围内使用多个参数呢?对于单列过滤来说很简单,但是对于下面的场景,该如何处理呢? 按邮政
我正在使用最新的 ruby+rails,以及 filterrific gem。它工作得很好 - 但如何在每个范围内使用多个参数呢?对于单列过滤来说很简单,但是对于下面的场景,该如何处理呢? 按邮政
我正在尝试编写一个搜索方法,用于搜索文章的全文,并按搜索词在全文中出现的次数对结果进行排序。像这样的事情: def search term = params[:term] @articles
创建连接表时如下 class Project { select('users.*, time_pledges.hours_pledged as hours_pledged')}, through:
我在理解整体概念及其在 Rails 中的工作原理方面遇到了一些困难,我认为它可以为您完成一些工作,因为我习惯于手动构建关联。我目前有一个计费系统,我正在尝试创建一个订单部分,显然我的订单必须有许多产品
这是我的 application.html.erb 代码:
所以我有一个 .csv 文件,我已将其导入到一个数组中。它们都是用逗号分隔的,所以我继续为 em 制作了一个漂亮的数组。 现在我正在尝试查找具有匹配 ID 的记录,这样我就可以删除重复项并只保留最后遇
使用 ActiveRecord 执行以下 SQL 的最佳方法是什么: SELECT parent.* FROM sections AS node, sections AS parent WHERE n
我想在 gem 中的方法之一中添加一些自定义代码。在 Rails 2.3.8 中,我在 config/initializers 目录下添加了一个 .rb 文件,一切正常。 自从转移到 Rail3 后,
试图了解 :has_many 的影响正如它在敏捷 Web 开发书第 4 版中所介绍的那样 为购物车设置以下关系 class Cart :destroy end 这恭维了相关的 LineItem类(c
在所有关于 Rails 3 的麻烦之后,我是否可以在几乎不更改我的 ActiveRecord 代码的情况下轻松使用 DataMapper,以便我可以在 GAE 上运行我的 Rails 而不会有任何麻烦
使用 Rails 2/3,可以使用几个插件之一(见 Best way to export a database table to a YAML file?)轻松地将数据库导出(转储)到 YAML。 但
我的布局中有这样的东西 ... ... 'user/bar' %> 在 user/bar.html.erb 中我有 stuff 这似乎不起作用。我发现 yield :test 在部分之前执行,
我使用 https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview 集成了 omniauth-facebook| .但我收到以下
我有一个 Rider 表,其中包含他们的城市。我正在从数据库中提取与当前用户所在城市匹配的条目。 这就是我在 Controller 中写的内容: @riders = Rider.where("city
我是一名优秀的程序员,十分优秀!