10

I have a Phone model nested to Message model. How can I find all Messages by a given number considering that number attribute is inside Phone model and not in Message?

This is what I currently got

class Message < ActiveRecord::Base
   attr_accessible  :phone_id
   belong_to :phone
end

class Phone < ActiveRecord::Base
   attr_accessible  :phone
   has_many :messages
end
4

2 に答える 2

21
Message.joins(:phone).where(phones: { phone: '555-555-5555' })
于 2012-05-25T17:18:48.567 に答える
5
Message.joins(:phone).where("phones.phone = ?","123-456-7890").all
于 2012-05-25T17:14:42.840 に答える