0

私が持っているもの:

条件付きSQLリクエスト.where

.where(:users => {:articles => {...}, :settings => {...}, ...})

必要なもの:

追加の条件なので、idof:usersは等しくありません@user.id

私が試したこと:

.where(:users => {['id != ?', @user.id], :articles => {...}, :settings => {...}, ...})

問題は何ですか:

構文

4

3 に答える 3

2

あなたが試すかもしれないこと:

.where(:users => {:articles => {...}, :settings => {...}, ...}).where('users.id != ?', @user.id)
于 2012-10-02T17:16:23.253 に答える
1

私は、belongs_to Place というファクト モデルを持っていますが、これは機能します。

Fact.includes(:place).where("places.id != 5")
于 2012-10-02T17:15:22.577 に答える
1

構文全体を改善できます

@users = User.scoped
if you_have_conditions_on_articles
  @users = @users.join(:articles).where(...conditions_on_article...)
end
if you_have_conditions_on_settings
  @users = @users.join(:settings).where(...conditions_on_settings...)
end
if you_need_to_exclude_user
  @users = @users.where('id != ?', @user.id)
end

@users

これにより正しいクエリが作成され、必要のない場合は条件を追加することを避けることができます。

クエリは、ループするまで実行されません (したがって、クエリがどのビューでも使用されていない場合、クエリはまったく実行されません)。

于 2012-10-02T17:47:02.673 に答える