Ruby on rails 在Rails 3中使用作用域
我试图在我的帐户模型中定义一个范围,但它不起作用。这是我的密码:Ruby on rails 在Rails 3中使用作用域,ruby-on-rails,ruby-on-rails-3,sqlite,Ruby On Rails,Ruby On Rails 3,Sqlite,我试图在我的帐户模型中定义一个范围,但它不起作用。这是我的密码: class Account < ActiveRecord::Base has_many :organizations scope :primary, joins(:organizations).where('organizations.primary = ?', true) accepts_nested_attributes_for :organizations end class Organ
class Account < ActiveRecord::Base
has_many :organizations
scope :primary, joins(:organizations).where('organizations.primary = ?', true)
accepts_nested_attributes_for :organizations
end
class Organization < ActiveRecord::Base
belongs_to :account
has_many :locations
accepts_nested_attributes_for :locations
end
但我得到了以下错误:
ActiveRecord::StatementInvalid: SQLLite3::SQLException: near "primary":
syntax error: SELECT "accounts".* FROM "accounts" INNER JOIN "organizations" ON
"organizations"."account_id" = "accounts"."id" WHERE (organizations.primary = 't')
LIMIT 1
我认为“primary”这个名字可能是问题的根源。当我将范围重命名为“重要”并尝试获得:
NoMethodError: undefined method 'important' for #<Class:0x1f4a900>
NoMethodError:未定义的方法“重要”#
如果有人能帮忙,我将非常感激 我认为您的问题在于您有一个名为“primary”的列,这是一个。试着引用它:
scope :primary, joins(:organizations).where('organizations."primary" = ?', true)
此例外情况:
SQLLite3::SQLException:靠近“主”:
来自SQLite,而不是ActiveRecord或Rails。不,引用不成功。还有其他想法吗?谢谢你的帮助。@Corey:相同的例外还是新的例外?相同的例外。但是我把“primary”列改为“primary_organization”,它可以工作。非常感谢。
scope :primary, joins(:organizations).where('organizations."primary" = ?', true)