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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:41:43+00:00 2026-06-14T01:41:43+00:00

I have an AR association with extensions in Rails similar to the example presented

  • 0

I have an AR association with extensions in Rails similar to the example presented in this link:
ActiveRecord Association Extensions

has_many :issues, :through => :qbert_issues do
  def tracking
    where("qbert_issues.kind = ?", "tracking")
  end

  def blocking
    where("qbert_issues.kind = ?", "blocking")
  end
end

As shown above, mine is multi-typed… I need to populate a ‘kind’ column in my join table. Ideally, this should just work:

q = QBert.find(123)
q.issues.tracking << Issue.find(234)

So, what the article suggests is overloading << and doing something like this:

has_many :issues, ... do
  ...
  def <<(issue)
    issue.kind = "UserAccount"
    proxy_association.owner.issues += [issue]
  end
end

Which would be nice, if kind was static.

It looks like I can do this…

has_many :issues, ... do
  ...
  def <<(*args)
    issue, kind = args.flatten
    issue.kind = kind
    proxy_association.owner.issues += [issue]
  end
end

Which would allow me to do this at the very least:

q = QBert.find(123)
q.issues.tracking << [Issue.find(234), :tracking]

That doesn’t seem very DRY to me…is there a better way? Bonus points if you take into account that the kind accessor is off a join table qbert_issues. I’m guessing I just have to add the association manually through the QBertIssue model directly.

  • 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-14T01:41:44+00:00Added an answer on June 14, 2026 at 1:41 am

    Figured it out…

    def <<(issue)
      kind = where_values.second.scan(/kind = '(.*)'/).flatten.first
      left = proxy_association.reflection.source_reflection
      right = proxy_association.reflection.through_reflection
    
      left.active_record.create(left.foreign_key.to_sym => issue.id, 
                                right.foreign_key.to_sym => proxy_association.owner.id, 
                                :kind => kind)
    end
    

    Which lets me do:

    q = QBert.find(123)
    q.issues.tracking << Issue.find(234)
    

    It could be made sufficiently generalized by parsing out the where_values and merging them into the parameters hash.

    Pry rocks, by the way 😀

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

Sidebar

Related Questions

I have something like this: class Person < ActiveRecord::Base has_many :things do def [](kind)
It looks like association extensions have changed quite a bit throughout Rails 3.x. I
I have this association-list in Common Lisp: (defvar base-list (list (cons 'a 0) (cons
I have an association between a project and a task. A project has_many taks
I have the following model association: a student model and has_many scores. I need
I have a question concerning active record association, referring to this part of the
I'm new to ruby on rails. So i think i have association problems. Given
From a practical and best practices perspective, should rails models that have HABTM association
Is it possible to have an association mapping a table to itself? e.g. Table:
Suppose we have two tables Foo and Bar. I have an association table Foo_Bar

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.