简体   繁体   English

为具有 3 个以上模型的关联创建 SCOPE - Rails 4

[英]Creating a SCOPE for an association with more than 3 models - Rails 4

Could one kindly advise me where i am going wrong with my SQL query可以请告诉我我的 SQL 查询哪里出错了

i want to display a company's list of forms that only have an accepted status @company.forms.created_this_month.forms_accepted.count but i get the below error.我想显示只有接受状态@company.forms.created_this_month.forms_accepted.count表单列表,但出现以下错误。 i am unsure where i am going wrong - any advise and help will be much appreciated我不确定我哪里出错了 - 任何建议和帮助将不胜感激

error in terminal:
2.1.2 :043 >   company.forms.created_this_month.forms_accepted
  Form Load (3.4ms)  SELECT "forms".* FROM "forms" INNER JOIN "adverts" ON "forms"."advert_id" = "adverts"."id" INNER JOIN "users" ON "adverts"."user_id" = "users"."id" WHERE "users"."company_id" = ? AND ("forms"."created_at" BETWEEN '2016-03-01 00:00:00.000000' AND '2016-03-31 22:59:59.999999') AND (status = 'Accepted')  [["company_id", 1]]
SQLite3::SQLException: ambiguous column name: status: SELECT "forms".* FROM "forms" INNER JOIN "adverts" ON "forms"."advert_id" = "adverts"."id" INNER JOIN "users" ON "adverts"."user_id" = "users"."id" WHERE "users"."company_id" = ? AND ("forms"."created_at" BETWEEN '2016-03-01 00:00:00.000000' AND '2016-03-31 22:59:59.999999') AND (status = 'Accepted')
ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: status: SELECT "forms".* FROM "forms" INNER JOIN "adverts" ON "forms"."advert_id" = "adverts"."id" INNER JOIN "users" ON "adverts"."user_id" = "users"."id" WHERE "users"."company_id" = ? AND ("forms"."created_at" BETWEEN '2016-03-01 00:00:00.000000' AND '2016-03-31 22:59:59.999999') AND (status = 'Accepted')


models
company.rb
has_many users
has_many :forms, through: :users

user.rb
belongs_to company
has_many adverts

advert.rb
belongs_to user
has_many forms

form.rb
belongs_to advert
scope :forms_accepted, -> {where(['status = ?', 'Accepted'])}
scope :created_this_month, -> { where(created_at: Time.now.beginning_of_month..Time.now.end_of_month) }


schema
  create_table "companies", force: true do |t|
    t.string   "companyname"
    t.string   "tel"
    t.string   "email"
  end

  create_table "users", force: true do |t|
    t.string   "email"
    t.string   "firstname"
    t.string   "lastname"
    t.integer  "company_id"
  end

  create_table "adverts", force: true do |t|
    t.string   "title"
    t.text     "content"
    t.integer  "user_id"
  end

  create_table "forms", force: true do |t|
    t.string   "firstname"
    t.string   "lastname"
    t.string   "email"
    t.string   "currentJob"
    t.string   "currentEmployer"
    t.integer  "advert_id"
    t.string   "status"
  end

Instead of:代替:

scope :forms_accepted, -> {where(['status = ?', 'Accepted'])}

use

scope :forms_accepted, -> {where(status: 'Accepted'])}

just as you used in the second scope.就像您在第二个范围中使用的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM