繁体   English   中英

SQLite3 :: SQLException:Rails 4.2.6中没有这样的列

[英]SQLite3::SQLException : no such column in rails 4.2.6

运行我的应用程序时,我遇到此SQLite异常错误:

SQLite3 :: SQLException在/ admin

没有这样的列:subscription_plan.name

这是控制器代码,我面临错误:

def dashboard
  @number = {
      :month => Subscription.includes(:subscription_plan).where(['subscriptions.created_at > ? AND subscriptions.amount > ? AND subscription_plan.name = ?', Time.zone.now.beginning_of_month, 0, 'month']).count,
      :year => Subscription.includes(:subscription_plan).where(['subscriptions.created_at > ? AND subscriptions.amount > ? AND subscription_plan.name = ?', Time.zone.now.beginning_of_month, 0, 'year']).count
    }
end

这是具有以下关系的订阅模型:

class Subscription < ActiveRecord::Base
  belongs_to :subscription_plan
end

我的应用程序中没有subscription_plan模型,但此表中的数据是通过seed.rb文件填充的。 如控制器中所示,“ month”,“ year”是subscription_plan表中的一些名称。

它可以在Rails 3.2上正常工作,我不知道为什么它不能在4.2.6下工作。 请帮忙。

您需要提供条件中的表名而不是关联名,请尝试

Subscription.includes(:subscription_plan).where(['subscriptions.created_at > ? AND subscriptions.amount > ? AND subscription_plans.name = ?', Time.zone.now.beginning_of_month, 0, 'month'])

你可以试试这个吗?

Subscription.includes(:subscription_plan).where(["subscriptions.created_at > ? AND subscriptions.amount > ? AND subscription_plans.name = ?", Time.zone.now.beginning_of_month, 0, 'month']).references(:subscription_plans)

您无需在where子句中引用订阅,请注意复数subscription_plans.name ,并且从power中获得的注释就可以正确地添加references(:subscription_plans)

Subscription.includes(:subscription_plan).where(['created_at > ? AND amount > ? AND subscription_plans.name = ?, Time.zone.now.beginning_of_month, 0, 'month']).references(:subscription_plans).count

暂无
暂无

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

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