2
我有3種表&型號: 品牌 brand_data_records 和 brand_data_records_brands - 連接表rails和sql其中IS NOT NULL無法正常使用連接。 Rails的2.3.5和Ruby 1.8.7 - MySQL的5
在鐵軌我想對於一個給定的日期範圍內的所有brand_data_records給定品牌,其中給定的屬性在數據庫中不爲空。
所以我必須:
BrandDataRecord.find(:all, :select => column_match, :joins => :brands, :conditions => ["brand_data_records_brands.brand_id = ? and date_retrieved >= ? AND date_retrieved <= ? and ? IS NOT NULL",brand.id,start_date,end_date,column_match])
這會產生這樣的SQL:
SELECT sentiment FROM `brand_data_records` INNER JOIN `brand_data_records_brands` ON `brand_data_records_brands`.brand_data_record_id = `brand_data_records`.id INNER JOIN `brands` ON `brands`.id = `brand_data_records_brands`.brand_id WHERE (brand_data_records_brands.brand_id = 330516084 and date_retrieved >= '2011-05-02' AND date_retrieved <= '2011-06-01' and 'sentiment' IS NOT NULL)
一般的作品,但它給回一堆有一個空值額外的記錄。我認爲它與連接有關,如果我刪除他們與SQL只有它工作正常,但我不知道如何解決在rails(甚至在sql中的事實)
我嘗試了符號的方法和MySQL是:SELECT情緒FROM'brand_data_records' INNER JOIN'brand_data_records_brands' ON'brand_data_records_brands'.brand_data_record_id ='brand_data_records'.id INNER JOIN'brands' ON'brands'.id ='brand_data_records_brands' .brand_id WHERE(brand_data_records_brands.brand_id = 330516084和date_retrieved> ='2011-05-02'AND date_retrieved <='2011-06-01'and'---:sentiment \ n'IS NOT NULL),並且具有相同的問題。我確實在sql字符串中嘗試了硬編碼的情緒,並且工作正常,所以你正處於問題的正確軌道上... – Joelio 2011-06-01 21:36:58
如果我讓情緒成爲符號並執行此操作:matching_records = BrandDataRecord.find(:all,:select = > column_match,:joins =>:brands,:conditions => [「brand_data_records_brands.brand_id =?and date_retrieved> =?AND date_retrieved <=?and#{column_match} IS NOT NULL」,brand.id,start_date,end_date]) 然後它工作。這看起來很醜陋,但至少可以工作! – Joelio 2011-06-01 21:38:29
是的,我的意思是「一個字符串永遠不會是NULL」,而不是永遠不會。 – tadman 2011-06-02 15:54:38