ActiveRecord, Find by value of nested attribute

I have a Phone model nested in the Message model. How to find all messages by a given number, given that the number attribute is inside the Phone model, and not in Message ?

This is what I got now

 class Message < ActiveRecord::Base attr_accessible :phone_id belong_to :phone end class Phone < ActiveRecord::Base attr_accessible :phone has_many :messages end 
+7
source share
2 answers
 Message.joins(:phone).where("phones.phone = ?","123-456-7890").all 
+6
source
 Message.joins(:phone).where(phones: { phone: '555-555-5555' }) 
+13
source

All Articles