Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails query with delegate fields of a model

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.

like image 934
Raj Adroit Avatar asked Apr 23 '14 06:04

Raj Adroit


1 Answers

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")
like image 67
Siva Avatar answered Sep 29 '22 08:09

Siva