Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 515157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:35:57+00:00 2026-05-13T07:35:57+00:00

Using has_many => through association. Here is what i have. :planning model has_many :acttypes

  • 0

Using has_many => through association.

Here is what i have.

:planning model

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

:acts model

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

:actcategories model

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

:acttype model

has_many :acts

My Problem Starts here. I need to show all Acts by each Act Type from Plannings that is part of the actcategories association
Right now i am getting all the acts and missing the actcategories association.

Planning Controller

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

Planning Show View

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

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:35:57+00:00Added an answer on May 13, 2026 at 7:35 am

    I think the key thing you’re missing is that finders and named scopes only return the Class that they’re called on.

    @acts = Actcategory.theacts(@planning)
    

    @acts is all the Actcategories where actcategories.planning_id = @planning.id. They don’t necessarily have the required act type.

    Really, what I think you’re looking for is this named scope:

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

    Which limits acts to those associated with the given planning. This can be called on an association to limit the linked acts to those associated with a specific planning.

    Example: @acts contains acts of acttype, x, that are associated with planning, y.

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

    With this named scope this code should accomplish what you were aiming for.

    controller:

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

    view:

    <% @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%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use a different formatting style to include the… May 14, 2026 at 7:48 pm
  • Editorial Team
    Editorial Team added an answer I don't think so. You need to populate your bsUsers,… May 14, 2026 at 7:48 pm
  • Editorial Team
    Editorial Team added an answer If you have separate activities for each page you want… May 14, 2026 at 7:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.