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 6058081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:33:36+00:00 2026-05-23T08:33:36+00:00

I have an author, who writes products, which are placed in groups. What I

  • 0

I have an author, who writes products, which are placed in groups.
What I want to do is to list the groups that an author is involved into.

So, I have first to follow the link from author to products, which works with :

@author = Author.find(params[:id])
@products = @author.products

which gives lot of products 🙂 Then I have to find (all) the groups that are linked to all of the products (because a group can contain a lot of products, I have already a has_many link with group_id column in the product table)

But when I try to write something as

@groups = @author.products.groups

I get the error : undefined method `groups’ for # Class:0x000000032a2198

Why ?

Here is the model, where only the “through” clauses seem not to work ?

class Author < ActiveRecord::Base
  has_and_belongs_to_many :products
  has_many :groups, :through => :products
end
class Product < ActiveRecord::Base
  belongs_to :group
  has_and_belongs_to_many :authors
end
class Group < ActiveRecord::Base
  has_many :products, :dependent => :destroy
  has_many :authors, :through => :products
end

Thanks !

Edit : I found an ugly way to do what I want. But is there no better RoR way ??

def show
@author = Author.find(params[:id])
    @products = @author.products
    @groups = []
    @products.each do |product|
        group = Group.find(product.group_id)
        unless @groups.include?(group)
            @groups << group
        end
    end
end
  • 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-23T08:33:36+00:00Added an answer on May 23, 2026 at 8:33 am

    As far as I know there’s no way to do it in a Rails way. First, here’s how to make what you have more “Railsy”:

    def show
      @author = Author.find(params[:id])
      @products = @author.products
      @groups = []
      @products.each do |product|
        unless @groups.include?(product.group)
          @groups << group
        end
      end
    end
    

    But a shorter way would be:

    @products.map(&:group).uniq
    

    Quick explanation of what it’s doing. First it’s using the symbol to proc shorthand that was introduced in Rails 1.1 (http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand). That builds an array of groups. Then b/c, in your question, you are checking for uniqueness (using include?) I call .uniq which weeds out any duplicates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some information in my database like 'author', 'book' etc., that are all
SQL query to get list of Authors who have written at least a book
I have a content type called Author which is referred in another two content
I want to install an outlook add-in for a user who does not have
I’im programing in Java with Netbeans. I have a table who list patients. I
I have column with the following information: Author varchar(255) utf8_general_ci the programmer who designed
I have created a blog app which its model has an author field like
As an Actionscript programmer shifting to JS/jQuery I often have to author multipage apps
I have two tables: author (id, first_name, last_name) books (id, title, rate, author_id) and
I have three tables Author, Book and AuthorBook. AuthorBook table only contains two foreign

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.