Rails Association Problem
Posted
by looloobs
on Stack Overflow
See other posts from Stack Overflow
or by looloobs
Published on 2010-03-20T04:37:54Z
Indexed on
2010/03/20
4:41 UTC
Read the original article
Hit count: 250
I am having trouble with this association.
I need to get an array of the primaries that belong to the soldiers in a platoon. So once I get all the soldiers in a platoon: @company = Company.find_by_id(1) @platoons = @company.platoons
<% @platoons.each do |p| %>
<%= p.soldiers.primaries.find(:all,:conditions => ["relationship = ? AND contacted = ?", 'Spouse', 'Yes'])) %>
<% end %>
* So there is no method for primaries, I assume this is because I am trying to call an association on an array. Soldiers have a platoon_id but primaries do not, they only have the association to soldiers in that platoon. How do I do this? I need it to return an array of Primaries. Thanks in advance!
class Soldier < ActiveRecord::Base
belongs_to :company
belongs_to :platoon
has_many :primaries, :dependent => :destroy
end
class Platoon < ActiveRecord::Base
belongs_to :company
belongs_to :battalion
has_many :soldiers
end
class Primary < ActiveRecord::Base
belongs_to :soldier
belongs_to :company
end
© Stack Overflow or respective owner