2017-07-31 38 views
0

我的應用程序列出了企業活動,並允許用戶使用通過回形針添加的可選照片編寫有關這些活動的報告。返回存在回形針附件的相關記錄

我想返回一個有附件的關聯報告列表。

我的事件has_many :reports

我的報告belongs_to :event和:

has_attached_file :photo, :styles => { :thumb => "150x150#" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :photo, :content_type => %w(image/jpeg image/jpg image/png)

我試圖返回已通過這樣的event_controller「秀」行動的附本人照片的所有相關報道:

@hasphoto = @event.reports.where.not(:photo => nil)

但我是得到以下錯誤:

Mysql2 ::錯誤:'where子句'中的未知列'reports.photo':SELECT'reports'。* FROM'reports'WHERE'reports.event_id = 13 AND('reports'。 'photo'is NOT NULL)

有人能告訴我我做錯了什麼,或者如果我以錯誤的方式接近這個任務,我該如何接近它?

+0

試試'@ event.reports.photo?' – Pavan

回答

0

基於回形針的add_attachment來源,你的代碼應該是這樣的:

@hasphoto = @event.reports.where.not(photo_file_name: nil) 

而且,你必須在控制檯rails db運行(rake db爲Rails 4-)和類型的選項:

mysql> SHOW CREATE TABLE reports\G