铁路协会问题

发布于 2024-08-26 06:28:30 字数 803 浏览 4 评论 0原文

我在这个协会遇到了麻烦。

我需要获取属于排中士兵的初选数组。所以一旦我把所有士兵都编入一个排: @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2024-09-02 06:28:30

您可以遍历士兵:

<% @platoons.each do |platoon| %>
  <% p.soldiers.each do |soldier| %>
    <%= soldier.primaries.find(:all,:conditions => ["relationship = ? AND contacted = ?", 'Spouse', 'Yes'])) %>
   <% end %>
<% end %>

这不是最佳的或优雅的。更好的解决方案是:

class Platoon < ActiveRecord::Base
  belongs_to :company
  belongs_to :battalion
  has_many :soldiers
  has_many :primaries, :through => :soldiers
end

那么您的观点将是:

<% @platoons.each do |platoon| %>
  <%= platoon.primaries.find(:all,:conditions => ["relationship = ? AND contacted = ?", 'Spouse', 'Yes'])) %>
<% end %>

You could loop through the soldiers:

<% @platoons.each do |platoon| %>
  <% p.soldiers.each do |soldier| %>
    <%= soldier.primaries.find(:all,:conditions => ["relationship = ? AND contacted = ?", 'Spouse', 'Yes'])) %>
   <% end %>
<% end %>

That's not optimal or elegant. A better solution would be:

class Platoon < ActiveRecord::Base
  belongs_to :company
  belongs_to :battalion
  has_many :soldiers
  has_many :primaries, :through => :soldiers
end

Then your view would be:

<% @platoons.each do |platoon| %>
  <%= platoon.primaries.find(:all,:conditions => ["relationship = ? AND contacted = ?", 'Spouse', 'Yes'])) %>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文