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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:00:20+00:00 2026-05-10T22:00:20+00:00

In my application, a user has_many tickets. Unfortunately, the tickets table does not have

  • 0

In my application, a user has_many tickets. Unfortunately, the tickets table does not have a user_id: it has a user_login (it is a legacy database). I am going to change that someday, but for now this change would have too many implications.

So how can I build a ‘user has_many :tickets’ association through the login column?

I tried the following finder_sql, but it does not work.

class User  < ActiveRecord::Base   has_many :tickets,       :finder_sql => 'select t.* from tickets t where t.user_login=#{login}'   ... end 

I get a weird error:

ArgumentError: /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:402:in `to_constant_name': Anonymous modules have no name to be referenced by     from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2355:in `interpolate_sql'     from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:214:in `qualified_name_for'     from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:477:in `const_missing'     from (eval):1:in `interpolate_sql'     from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:95:in `send'     from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:95:in `interpolate_sql'     from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_association.rb:143:in `construct_sql'     from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_association.rb:6:in `initialize'     from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1032:in `new'     from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1032:in `tickets'     from (irb):1 

I also tried this finder_sql (with double quotes around the login):

:finder_sql => 'select t.* from tickets t where t.user_login='#{login}'' 

But it fails the same way (and anyway, if it worked it would be vulnerable to sql injection).

In a test database, I added a user_id column in the tickets table, and tried this finder_sql:

:finder_sql => 'select t.* from tickets t where t.user_login=#{id}' 

Now this works fine. So apparently, my problem has to do with the fact that the users column I am trying to use is a string, not an id.

I searched the net for quite some time… but could not find a clue.

I would love to be able to pass any parameter to the finder_sql, and write things like this:

has_many :tickets_since_subscription, :finder_sql => ['select t.* from tickets t where t.user_login=?'+      ' and t.created_at>=?', '#{login}', '#{subscription_date}'] 

Edit: I cannot use the :foreign_key parameter of the has_many association because my users table does have an id primary key column, used elsewhere in the application.

Edit#2: apparently I did not read the documentation thoroughly enough: the has_many association can take a :primary_key parameter, to specify which column is the local primary key (default id). Thank you Daniel for opening my eyes! I guess it answers my original question:

has_many tickets, :primary_key='login', :foreign_key='user_login' 

But I would still love to know how I can make the has_many :tickets_since_subscription association work.

  • 1 1 Answer
  • 3 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. 2026-05-10T22:00:20+00:00Added an answer on May 10, 2026 at 10:00 pm

    I think you want the :primary_key option to has_many. It allows you to specify the column on the current Table who’s value is stored in the :foreign_key column on the other table.

    has_many :tickets, :foreign_key => 'user_login', :primary_key => 'login' 

    I found this by reading the has_many docs.

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

Sidebar

Related Questions

I have a simple rails application were book belongs_to user and user has_many book.
I am creating a web application that will have many users. Each user has
I have a desktop application where the user has a library of encrypted ZIP
I have a field in my application where the user has to type the
I have a web application in which a user has to upload images to
I've got an old application that has user passwords stored in the database with
Suppose I have a database containing 3 tables in a grails application: User Activity
I have a blogging application in which User has_many posts. I am using pagination
I have a Rails application with the following models: User Bet User has many_bets
In my application I have a very simple association. A User has_many Emails, this

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.