Twiddr
Twiddr

Reputation: 297

Loop through objects to find a match

I have a Shift and a Type table with the following relations:

class Shift < ActiveRecord::Base
  belongs_to :type
end

class Type < ActiveRecord::Base
  has_many :shifts, :dependent => :destroy
end

The Type table has a attribute called Unassignable.

I would like to do a query which returns all the Shifts which belongs to a Type where the Unassignable attribute is true..

Something like: Shift.where(:shift.type.unassignable => true)

Any help :)

Thanks

Upvotes: 1

Views: 123

Answers (1)

Sachin R
Sachin R

Reputation: 11876

Shift.includes(:type).where("types.unassignable = ?", true)

Upvotes: 1

Related Questions