铁路协会问题
我在这个协会遇到了麻烦。
我需要获取属于排中士兵的初选数组。所以一旦我把所有士兵都编入一个排: @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 %>
*** 所以没有用于初选的方法,我认为这是因为我试图在数组上调用关联。士兵有 platoon_id 但 primaries 没有,他们只与该排中的士兵有关联。我该怎么做?我需要它返回一个原色数组。提前致谢!
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以遍历士兵:
这不是最佳的或优雅的。更好的解决方案是:
那么您的观点将是:
You could loop through the soldiers:
That's not optimal or elegant. A better solution would be:
Then your view would be: