0

次のように、 User モデルでクエリを実行したいと思います。

@user = User.find(:all, :conditions=>["u_org != c_org and u_org == ?", current_web.role.id])

これはクエリを実行するための古いスタイルであり、同様に機能していません。Rails 3で実行するのを手伝ってくれませんか。私は試しました:

@user = User.where("u_org != c_org and u_org == ?", current_web.role.id).all

結果は[]でした。

アップデート:

==回答に従って削除しましたが、それでもクエリは を返します[]。私はでのみ試してみ@user = User.find(:all, :conditions=>["u_org = ?", current_web.role.id])ましたが、値は正常に返されましたが、それをしたいです@user = User.find(:all, :conditions=>["u_org != c_org and u_org = ?", current_web.role.id])

編集:

サンプルデータ:

u_org = 1

c_org = null

current_web.role.id = 1

4

2 に答える 2

0

問題はNULLが原因だと思います.nullにc_org等しくないすべてのユーザーをnullに使用c_org IS NOT NULLし、 nullに等しいユーザーを使用する場合c_orgc_org IS NULL

@user = User.find(:all, :conditions=>["c_org IS NOT NULL and u_org = ?", current_web.role.id])

編集済み

@user = User.find(:all, :conditions=>["(u_org != c_org OR c_org IS NULL) and u_org == ?", current_web.role.id])
于 2013-08-07T11:09:02.923 に答える