Pregunta

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.

¿Fue útil?

Solución

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")
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top