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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:47:08+00:00 2026-05-17T02:47:08+00:00

So Rails doesn’t have support for :through associations through a habtm relationship. There are

  • 0

So Rails doesn’t have support for :through associations through a habtm relationship. There are plugins out there that will add this in for Rails 2.x, but I’m using Rails 3 / Edge, and only need the association for one particular model. So I thought I’d stump it out myself with the beauty that is Arel.

First, the models:

class CardSet < ActiveRecord::Base
  has_and_belongs_to_many :cards, :uniq => true
end

class Card < ActiveRecord::Base
  has_and_belongs_to_many :card_sets, :uniq => true
  has_many :learnings, :dependent => :destroy
end

class Learning < ActiveRecord::Base
  belongs_to :card
end

I want to get all the learnings that belong to a particular card set, :through => cards.

This is what I have so far in my CardSet model:

def learnings
  c = Table(Card.table_name)
  l = Table(Learning.table_name)
  j = Table(self.class.send(:join_table_name, Card.table_name, CardSet.table_name))
  learning_sql = l.where(l[:card_id].eq(c[:id])).where(c[:id].eq(j[:card_id])).join(j).on(j[:card_set_id].eq(self.id)).to_sql
  Learning.find_by_sql(learning_sql)
end

which gives me (damn, Arel is beautiful!):

SELECT     `learnings`.`id`, `learnings`.`card_id`, `learnings`.`user_id`, `learnings`.`ef`, `learnings`.`times_seen`, `learnings`.`next_to_be_seen`, `learnings`.`interval`, `learnings`.`reps`, `learnings`.`created_at`, `learnings`.`updated_at`, `card_sets_cards`.`card_set_id`, `card_sets_cards`.`card_id` FROM       `learnings` INNER JOIN `card_sets_cards` ON `card_sets_cards`.`card_set_id` = 1 WHERE     `learnings`.`card_id` = `cards`.`id` AND `cards`.`id` = `card_sets_cards`.`card_id`

which is oh so close to what I’m aiming for – just needs to add in the cards table in the FROM part of the statement.

And there’s my question: I’ve looked through the Arel source, and for the life of me I can’t figure out how to add in another table.

As a sidenote, is there a better way to run the result from Arel (which usually returns an Arel::Relation, which I don’t want) through ActiveRecord, other than rendering to sql and running the query with find_by_sql as I’m doing?

  • 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-17T02:47:09+00:00Added an answer on May 17, 2026 at 2:47 am

    This is easier than what I wrote above, after I learned how Rails encapsulates Arel so you don’t need to build the query directly.

    If you’re trying to stub nested habtm in the CardSet model, a.k.a.:

    has_many :learnings, :through => :cards
    

    then you can use this to simulate the behaviour:

    class CardSet < ActiveRecord::Base
    
      has_and_belongs_to_many :cards, :uniq => true
    
      def learnings
        Learning.joins(:card).where(:cards => { :id => self.card_ids })
      end
    end
    

    then a query like this will work:

    CardSet.first.learnings
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rails is a great platform, but it just doesn't have the history that Perl
What's the best way to create a model in Ruby on Rails that doesn't
I'm building a rails gallery app for a company that doesn't want to host
Does Rails 2.3 support a file-based session store out of the box any more?
It seems rails 3 doesn't have rake gems:unpack , what we should use instead?
I have a Rails application that unfortunately after a request to a controller, has
I'm trying to come up with things that Ruby (or Rails) either doesn't handle
I have a Rails 3 gem which has some rake tasks that should only
Is there any one-liner commands that does this for me because bundle with rails
Complete newbie researching Rails. Can Rails be used with a read-only schema that doesn't

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.