views:

25

answers:

1

I am having a problem with an association:

Battalion :has_many soldiers

Soldiers :has_many primaries

I need to do this @bseniorleads=(@user.battalion.soldiers.find(:all, :conditions => ["seniorleader = ?", "Yes"])) then @seniorspouse=(@bseniorleads.primaries.find(:all, :conditions => ["relationship = ?", "Spouse"]

This gives me an undefined method for primaries, I assume because the bseniorleads is an array?

Basically I don't know how to do this they right way but I need to be able to query a group from one model that meets a condition and then take that result and find the people from another model that belong to them. Any ideas?

Thanks in advance.

A: 

You should be able to do something like this (assuming you only needed the @bseniorleads instance variable in the second query):

@senior_spouse = @user.battalion.soldiers.find(
   :all,
   :select => 'primaries.*',
   :joins => [:primaries],
   :conditions => ["seniorleader = ? and primaries.relationship = ?", "Yes", "Spouse"]
)

I haven't checked that yet, but I think it should get you pretty close.

You might want to check out these two rails guides, which certainly helped me better understand ActiveRecord associations and querying:

jerhinesmith
Thank you so much, I have checked out the guides and they are a huge help, I am so new to programming that anything complex throws me. This has been a big help in understanding what I need to do and how it works.
looloobs