通过关联使用 has_many =>。
这就是我所拥有的。
:规划模型
has_many :acttypes
has_many :actcategories
has_many :acts, :through => :actcategories
:行为模型
belongs_to :acttype
has_many :actcategories
has_many :plannings, :through => :actcategories
:actcategories 模型
named_scope :theacts, lambda { |my_id|
{:conditions => ['planning_id = ?', my_id] }}
belongs_to :act
belongs_to :planning
:acttype 模型
has_many :acts
我的问题从这里开始。我需要从属于 actcategories 关联的Plannings中按每个Act Type显示所有Act 现在我正在获取所有的 act 而缺少actcategories 关联。
计划控制器
def show
@planning = Planning.find(params[:id])
@acttypes = Acttype.find(:all, :include => :acts)
@acts = Actcategory.theacts(@planning)
end
规划展示视图
<% @acttypes.each do |acttype|%>
<%= acttype.name %>
<% @acts.each do |acts| %>
<li><%= link_to acts.act.name, myacts_path(acts.act, :planning => @planning.id) %></li>
<% end %>
<% end -%>
谢谢你的帮助。