문제

Hi am having two models with has_one association. I need to search with delegate fields.

**Model 1:**
class Reservation < ActiveRecord::Base
    belongs_to :content
    delegate :type, :title, :to => :content
end

**Model 2:**
class Content < ActiveRecord::Base
    has_one :reservation
end

The following query works fine, because of delegate:

reservations = Reservation.last
reservations.title
 ~   Content Load (0.6ms)  SELECT `contents`.* FROM `contents` WHERE `contents`.`id` = 95 LIMIT 1
 => "Birthday Party" 

Now i need to do query using delegate fields:

reservations = Reservation.where("title = ?","some_title")

it returns error:

Unknown column 'title' in 'where clause'

How can i solve the problem? is that am doing in correct way? Thanks for reading my question.

도움이 되었습니까?

해결책

Problem can solved by using joins and where. But not sure if this is what you are looking for.

reservations = Reservation.joins(:content).where("contents.title = ?","some_title")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top