From: Phrogz Date: 2009-03-31T05:24:58+09:00 Subject: Re: activerecord query building question On Mar 30, 12:30 pm, Aldric Giacomoni <"aldric[remove]"@trevoke.net> wrote: > I have this: > > require 'activerecord' > > class RefPhys < ActiveRecord::Base >     set_table_name "tbRefPhys" >     has_many :refphyslocations, :foreign_key => "refphysid" > end > > class RefPhysLocation < ActiveRecord::Base >     set_table_name "tbRefPhysLocations" >     belongs_to :refphys, :foreign_key => "refphysid" > end > > I want a list of the refphys names (in refphys) in a specific city (in > refphyslocation). For comparison with ActiveRecord (which I've all but forgotten now) here's how you could do this in the Sequel[1] library: RefPhys.filter( :refphysid => refphyslocation.id ) The cool thing about Sequel is that until you ask it to go and fetch the records, you have a Dataset object that you can keep filter. For example: correct_location = RefPhys.filter( :refphysid => refphyslocation.id ) # Further pare down the Dataset my_phys = correct_location.filter( :owner_id => me.id ) # Filter to match multiple criteria, one of which is an array of items my_open = my_phys.filter( :status => Status::OPEN_IDS, :hidden => false ) my_open.each do |refphys| # Now you've run the single query and are running through the results end [1] http://sequel.rubyforge.org/