簡體   English   中英

在模型中添加選擇將刪除.count功能(導軌)

[英]Adding select to model removes .count functionality (rails)

在創建聯接表時如下

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並獲得有關當時有多少用戶自願參與特定項目的答案。 但是,當我更新項目模型以能夠訪問聯接表模型的hours_pledged屬性( time_pledges )時,如下所示:

  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之后,我將無法再次訪問任何用戶(直到重新啟動控制台會話)。 例如,如果在@project.volunteers.count之后執行U5=User.find_by(id:5)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時,除非用於我要加入的select語句,否則我不會使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM