2011-08-16 13 views
2

アカウントモデルでスコープを定義しようとしていますが、機能していません。ここに私のコードは次のとおりです。Rails 3でスコープを使用する

Account.primary.first 

しかし、私は次のエラーを取得する:

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 

私は名前のプライマリ "だと思います問題を引き起こしている可能性があります。私は「重要」にスコープを名前を変更し、私が得ることを試みたとき:

NoMethodError: undefined method 'important' for #<Class:0x1f4a900> 

誰もが、私はそれを非常に感謝することができます。

答えて

2

「プライマリ」という名前の問題は、reserved wordです。それを引用してみてください。

scope :primary, joins(:organizations).where('organizations."primary" = ?', true) 

この例外:

SQLLite3::SQLException: near "primary":

がないのActiveRecordやレールから、SQLiteのから来ています。

+0

いいえ、引用符は運がありません。他のアイデア?ご協力いただきありがとうございます。 –

+0

@Corey:同じ例外または新しい例外? –

+0

同じ例外があります。しかし、私は 'primary'の列を 'primary_organization'に変更しました。ありがとうございました! –

関連する問題