Topic: Active Records

Hi,

I have one model Buniit

second model BunitSetting

Both models are related by

class BunitSetting < ActiveRecord::Base
  # Associations
  belongs_to :bunit
end

I want to execute following query,,

SELECT bunits.id,bunits.name
FROM bunits, bunit_settings
WHERE bunits.id = bunit_settingsbunit_id
AND bunit_settings.isregistrationopen =1

How do I do that using find method

@bunit = Bunit.find(:all , :condition => [????????????????????????])

what should I give in the condition to produce the same result as query.

Last edited by saurabh (2009-11-19 04:11:49)

Re: Active Records

bunit = BUnit.find(your_bunit_id)
unless bunit.nil?
   bunit_settings = bunit.bunit_setings.select {|setting| setting.isregistrationopen ==1}
end 

Or if your 'isregistrationopen' value is boolean as I suppose, yuo can do it like that:

bunit = BUnibt.find(your_bunit_id)
unless bunit.nil?
   bunit_settings = bunit.bunit_setings.select {|setting| setting.isregistrationopen?}
end 

Like that you will get an Array of your BUnitSetting objects. If you loop on them, you can easily get whatever you need (id, name, etc.)

Last edited by Javix (2009-11-19 04:28:34)