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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:00:12+00:00 2026-05-27T01:00:12+00:00

I am having an issue with model associations. I want to pull a route’s

  • 0

I am having an issue with model associations. I want to pull a route’s trip info by accessing the data from trips.route_id where routes.route_id would be the same value. Currently the SQL query is calling routes.id, instead using routes.route_id. Any help would be appreciated.

Route Table Structure

COLUMNS: id,route_id,route_short_name,route_long_name,route_desc,route_type  
ROW: 1,2-36,2,"East 34th St",,3

The primary_key on this table is ‘id’.

Models

class Route < ActiveRecord::Base
  has_many :trips, :foreign_key => 'route_id'
end
class Trip < ActiveRecord::Base
  belongs_to :route
end

Route Controller

class RouteController < ApplicationController
  def show
    @route = Route.where("'%s' = routes.route_short_name",params[:id]).first
  end
end

Want to call @route.trips to pull trip information associated with said @route

LOG INFO

Started GET "/route/19" for 127.0.0.1 at Wed Nov 23 21:09:55 -0500 2011
Processing by RouteController#show as HTML
Parameters: {"id"=>"19"}
Route Load (0.4ms)  SELECT `routes`.* FROM `routes` WHERE ('19' = routes.route_short_name) LIMIT 1
Trip Load (8.4ms)  SELECT `trips`.* FROM `trips` WHERE `trips`.`route_id` = 15

Trip Load Explanation: 15 represents the id of the object returned from the “Route Load” query. I would like to use the routes.route_id value of the result instead of the id to build the “Trip Load” query.

Desired result:
Trip Load (8.4ms) SELECT 'trips'.* FROM 'trips' WHERE 'trips'.'route_id' = '2-36'
('2-36' value is referenced from Route Table Structure example)

  • 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-27T01:00:12+00:00Added an answer on May 27, 2026 at 1:00 am

    If I understand your question properly, you need to change your models to something like this:

    class Route < ActiveRecord::Base
      has_many :trips, :primary_key => "route_id"
    end
    
    class Trip < ActiveRecord::Base
      belongs_to :route, :primary_key => "route_id"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue viewing some model data within an inline ItemRenderer, in a
I'm having an issue accessing some nested data in an XML response using Perl/XML::Simple.
This time i'm having an issue with the actual rendering of my model. I
I'm having an issue where the model is valid when it creates the HttpGet
I'm having an issue when I try to update a task model to the
I am having an issue that coming from PHP 5.3.2 to 5.3.3 the code
I am currently having a strange issue with an application migrated from rails 2.3.8
i'm having an annoying issue with django model system + its default admin. Let's
Having a bit of a hairy issue when submitting data over a jquery ajax
I'm having a problem while trying to call a custom Model method from my

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.