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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:04:21+00:00 2026-05-26T05:04:21+00:00

When I join two tables (rails 2.2.2), then rails only returns the values of

  • 0

When I join two tables (rails 2.2.2), then rails only returns the values of the attributes from the Model for which the find_by method is applied:

Relations:

class User < ActiveRecord::Base
  ..
  has_one  :company_info, :dependent => :destroy
  has_many :preferred_categories, :dependent => :destroy
end


class PreferredCategory < ActiveRecord::Base
  ..
  belongs_to :user
  belongs_to :category

end


class Category < ActiveRecord::Base
  ..
  has_many :preferred_categories, :dependent => :destroy
  has_many :users, :through => :preferred_categories
end


class CompanyInfo < ActiveRecord::Base
  ..
  belongs_to :user 
end

Query:

class Category < ActiveRecord::Base
  ..
  def total_tradesmen   #returns tradesmen with a certain skill-profile
  a = self.self_and_all_children_for.collect {|cat| cat.id}
  total_tradesmen = User.find_by_sql(
    "select *
    from users u
    inner join preferred_categories pc on pc.user_id = u.id
    inner join company_infos ci on ci.user_id = pc.user_id
    where pc.category_id in ( #{a.join(',')} )"
  )
  end
..
end

=> Result: I get only attributes from the users table.

What I need are the attributes from the other tables (preferred_categories, company_infos) as well. Any ideas?

  • 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-26T05:04:22+00:00Added an answer on May 26, 2026 at 5:04 am

    In your case I think that ActiveRecord can handle your query. You should eager load the associations that you need by using include.

    total_tradesmen = User.all(
      :include => [:company_info, :preferred_categories],
      :conditions => {:preferred_categories => {:category_id => a}}
    )
    

    Then access the attributes you need on the associated models:

    # Examples since I don't know the attributes on your models
    company_address = total_tradesmen.first.company_info._address_
    preferred_category_names = total_tradesmen.first.preferred_categories.map(&:_name_)
    

    UPDATE
    Try specifying joins explicitly

    total_tradesmen = User.all(
      :include => [:company_info, :preferred_categories],
      :joins => [:company_info, :preferred_categories],
      :conditions => {:preferred_categories => {:category_id => a}}
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to join two tables (below) which i get. But then I want
I need to join two tables from different MySQL (PHP) connection strings and different
Actually if i am doing INNER JOIN of two tables then i will get
I'm porting a process which creates a MASSIVE CROSS JOIN of two tables. The
I have two tables which I want to join together using a left outer
I am using ActiveRecord in Rails 3 to pull data from two different tables
I am attempting to join two tables by comparing and matching values in separate
I want to join two tables together and have ONLY the data in Table
I want to left join two tables. My problem is that the table from
I want to join two tables to get data from both tables, for example,

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.