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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:12:07+00:00 2026-06-17T18:12:07+00:00

I have this relation in my Product model: has_many :features, :class_name => ‘ProductFeature’, :source

  • 0

I have this relation in my Product model:

has_many :features, :class_name => 'ProductFeature', :source => :product_feature, :include => :feature

So I can do Product.features

which works fine. But I want to be able to filter that by fields in the feature table, when and if necessary. For example in pseudo code:

find all product features where feature is comparable

compare is a bool field on the feature.

I have been trying for 2 hours solid and cannot figure it out (without writing a new query completely). I can’t figure out how to access the feature table’s fields from the Product.features relation, as it seems it can only filter on product_features fields.

This is what I have come up with so far:

def features_compare
  features.feature.where(:compare => true)
end

But it just says feature is not a valid method, which I understand.

Edit

I have updated my model so the relationships are clearer:

product.rb:

class Product < ActiveRecord::Base
  belongs_to :company
  belongs_to :insurance_type

  has_many :product_features
  has_many :reviews

  attr_accessible :description, :name, :company
end

product_feature.rb:

class ProductFeature < ActiveRecord::Base
  belongs_to :product
  belongs_to :feature

  delegate :name, :to => :feature

  attr_accessible :value
end

feature.rb

class Feature < ActiveRecord::Base
  attr_accessible :name, :compare
end

I want to be able to query the product_features that belong to a product and feature where Feature.compare is true. Something like this:

product.rb

def features_compare
  product_features.where(:compare => true)
end

This throws an error because compare in in the Feature model, not ProductFeature. I have tried the following in product_feature.rb:

delegate :compare, :to => :feature

but I didn’t help.

I will adding a bounty to this in a few hours so please please help me!

  • 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-17T18:12:08+00:00Added an answer on June 17, 2026 at 6:12 pm

    find all product features where feature is comparable is just

    ProductFeature.joins(:feature).where(:feature => {:compare => true})
    

    You can make that a bit more reusable by introducing a scope:

    #in product_feature.rb
    scope :with_feature_like, lambda do |filter|
       joins(:feature).where(:feature => filter)
    end
    
    #elsewhere
    ProductFeature.with_feature_like(:compare => true)
    
    #all the product features of a certain product with at comparable features
    some_product.product_features.with_feature_like(:compare => true)
    

    Finally, if you want all products with product features with comparable features, you want something like:

    Product.joins(:product_features => :feature).where(:feature => {:compare => true})
    

    which of course you can also turn into a scope on Product.

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

Sidebar

Related Questions

I have a Product model, with a one-to-many relation with a Rating model. I
I have this problem decomposing a relation schema into a set of schemas that
I have this table: itemA itemB relation 1 2 0.4 1 3 0.8 2
I have this mapper defined: mapper(Resource, resource_table, properties = {'type' : relation(ResourceType,lazy = False),
My question is in relation this this answer. https://stackoverflow.com/a/8773953/1297775 I have read at many
I have Cinema table & City table and I have relation with this tables
In relation to this question ( Efficient hashCode() implementation ) I have one more
I have a Profile model and two ActiveRecord::Relations of this model. SQL request of
I've got to classes Product and Store which have many to many relation I
I have this 3 tables in my DB: product (id, sku, type, price) ,

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.