Topic: Active Records


I have one model Buniit

second model BunitSetting

Both models are related by

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

I want to execute following query,,

FROM bunits, bunit_settings
WHERE = 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 = {|setting| setting.isregistrationopen ==1}

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 = {|setting| setting.isregistrationopen?}

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)