运行我的应用程序时,我遇到了这个SQLite异常错误:
/ admin
中的SQLite3 :: SQLException没有这样的专栏: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模型,但此表的数据是通过 seeds.rb 文件填充的。 如控制器中所示,&#39; month&#39; year&#39; year&#39;是subscription_plan表中的一些名称。
Rails 3.2工作正常,我不知道为什么它不能使用4.2.6。请帮忙。
答案 0 :(得分:2)
您需要提供条件而不是关联名称的表名,请尝试
Subscription.includes(:subscription_plan).where(['subscriptions.created_at > ? AND subscriptions.amount > ? AND subscription_plans.name = ?', Time.zone.now.beginning_of_month, 0, 'month'])
答案 1 :(得分:1)
请你试试这个。
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)
答案 2 :(得分:0)
您无需在where
子句中引用订阅,请注意复数subscription_plans.name
,并且从功能上来说,添加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