2016-06-10 22 views
1

我想查詢一個表中多個引用列不爲null。哪裏不是空的參考列顯示錯誤 - 鐵軌

我想是這樣的:( '?COMMENT_TEXT ==',無) JobTypePresetting.where JobTypePresetting.where( 'comment_text_id ==還是part_listing_id ==?',零,零)

即使第一工作不顯示錯誤,如

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: integer == unknown 
LINE 1: ...resettings"."template_id" = $2 AND (comment_text_id == NULL) 
                 ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 

如何查詢此,我目前使用像

JobTypePresetting.where('comment_text_id is not null or part_listing_id is not null') 
+0

你想一個等號,而不是雙。 –

回答

0

您需要使用單(= )操作員。

JobTypePresetting.where( 'COMMENT_TEXT =?',無)

希望這會爲你工作。

+0

您也可以使用 JobTypePresetting.where( 'COMMENT_TEXT IS NULL') OR JobTypePresetting.where(COMMENT_TEXT:無) –

0

嘗試使用Arel,更容易構建複雜的SQL查詢。他們已經從3版本的軌道的一部分。​​

jtp = JobTypePresetting.arel_table 
JobTypePresetting.where(jtp[:comment_text_id].not_eq(nil).or(jtp[: part_listing_id].not_eq(nil))) 

希望它可以幫助