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

  • Home
  • SEARCH
  • 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 9165123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:50:28+00:00 2026-06-17T14:50:28+00:00

I am trying to use the has_many :through relation in rails to return a

  • 0

I am trying to use the has_many :through relation in rails to return a set of product features. Please see this gist for the models: https://gist.github.com/4572661

I know how to do this using the ProductFeature model directly, but I really do not want to have to interact with it directly.

I want to be able to do this:


features = Product.features

So it returns:


[id: 1, name: 'Colour', value: 'Blue'],
[id: 2, name: 'Size', value: 'M'],
[id: 3, name: 'Shape', value: 'Round']

But I can only get it to return:


[id: 1, name: 'Colour'],
[id: 2, name: 'Size'],
[id: 3, name: 'Shape']

I was using this as a starting point.

  • 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-06-17T14:50:29+00:00Added an answer on June 17, 2026 at 2:50 pm

    has_many :through is designed to regard the join table as nothing more than that.

    Any columns on the join will be ignored from the association.

    As such we have to use product_features

    product.product_features(include: :feature)
    

    Thereby we can say

    product.product_features(include: :feature).each do |pf|
      feature = pf.feature
    
      name = feature.name
      value = pf.value
    end
    

    If you use this type of thing a lot, I’d be inclined to do something like this;

    class Product
      # always eager load the feature
      has_many :product_features, include: :feature
    end
    
    class ProductFeature
      # delegate the feature_name
      delegate :name, to: :feature
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up a many to many relationship using the has_many :through
I'm trying to use the :through method for the first time in rails, doing
I'm trying to use Tastypie with ManyToMany relationships using intermediate models ( through keyword)
I'm trying to use mock to verify that an index property has been set.
I'm trying to use Boost regex to see if something has an integer in
Currently, an Item belongs_to a Company and has_many ItemVariants . I'm trying to use
I am trying to use a nested form but keep getting this error when
I have three models: class Client < ActiveRecord::Base has_many :balancesheets has_many :investment_assets, :through =>
I'm trying to get to grips with this type of association in Rails I
I've got some trouble trying to make this work. I have 2 models, User_Pro

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.