سؤال

باستخدام has_many => من خلال جمعية.

هنا ما لدي.

: نموذج التخطيط

has_many :acttypes
has_many :actcategories
has_many :acts, :through => :actcategories

: الأفعال نموذج

belongs_to :acttype
has_many :actcategories
has_many :plannings, :through => :actcategories

: نموذج القضايا نموذج

named_scope :theacts, lambda { |my_id|
{:conditions => ['planning_id = ?', my_id] }} 
belongs_to :act
belongs_to :planning

: نموذج acttype

has_many :acts

مشكلتي تبدأ هنا. أحتاج إلى إظهار الكل أعمال من قبل كل منهما نوع القانون من Plannings. هذا هو جزء من جمعية أكتيف الفئاتالآن أنا أحصل على كل الأفعال وفقدت جمعية أكتيف الفئات.

تحكم التخطيط

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 -%>

شكرا على اي مساعدة.

هل كانت مفيدة؟

المحلول

أعتقد أن الشيء الرئيسي الذي تفتقده هو أن الباحثين والمكتشفون المسمى يعودون فقط الفئة التي يتم استدعاؤها.

@acts = Actcategory.theacts(@planning)

acts جميع الفئات حيث actcategories.planning_id = @planning.id. وبعد ليس لديهم بالضرورة نوع القانون المطلوب.

حقا، ما أعتقد أنك تبحث عن هو هذا النطاق المسمى:

class Act < ActiveRecord::Base
  named_scope :with_planning, lambda do |planning_id|
   { :joins => :actcategories, 
    :conditions => {:actcategories => {:planning_id => planning_id}}
   }
  ...
end

الذي يحد أن أعمالها المرتبطة بالتخطيط المحدد. يمكن استدعاء ذلك على جمعية للحد من الأفعال المرتبطة مع تلك المرتبطة بتخطيط محدد.

مثال: لا تحتوي acts على أعمال acttype، x، والتي ترتبط بالتخطيط، ذ.

@acts = Acttype.find(x).acts.with_planning(y)

مع هذا النطاق المسمى، يجب أن ينجز هذا الرمز ما كنت تهدف إليه.

مراقب:

def show
  @planning = Planning.find(params[:id])
  @acttypes = Acttype.find(:all, :include => :acts)
end

رأي:

<% @acttypes.each do |acttype| %>
<h2> <%= acttype.name %><h2>
  <% acttype.acts.with_planning(@planning) do |act| %>
    This act belongs to acttype <%= acttype.name%> and 
     is associated to <%=@planning.name%> through 
     actcatgetories: <%=act.name%>
  <%end%>
<%end%>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top