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

  • Home
  • SEARCH
  • 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 6617601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:43:25+00:00 2026-05-25T20:43:25+00:00

Having customer and category table. There are category1_id and category2_id in customer table. If

  • 0

Having customer and category table. There are category1_id and category2_id in customer table. If having @customer, how to fetch the category name by given @customer and linking the category1_id and category2_id with the id in category table?

customer schema:

  create_table "customers", :force => true do |t|
    t.string   "name"
    t.string   "short_name"
    t.string   "contact"
    t.string   "address"
    t.string   "country"
    t.string   "phone"
    t.string   "fax"
    t.string   "email"
    t.string   "cell"
    t.integer  "sales_id"
    t.string   "web"
    t.integer  "category1_id"
    t.integer  "category2_id"
    t.boolean  "active",         :default => true
    t.string   "biz_status"
    t.integer  "input_by_id"
    t.string   "quality_system"
    t.string   "employee_num"
    t.string   "revenue"
    t.text     "note"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

category schema:

  create_table "categories", :force => true do |t|
    t.string   "name"
    t.string   "description"
    t.boolean  "active",      :default => true
    t.datetime "created_at"
    t.datetime "updated_at"
  end

In routes.rb file

  resources :customers do
    resources :categories
  end

Given @customer, how to fetch category name, like @cusotmer(category1_id).category.name?

Thanks.

  • 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-25T20:43:25+00:00Added an answer on May 25, 2026 at 8:43 pm

    You have two single belongs_to associations in your model. Like this:

        class Customer < ActiveRecord::Base
          belongs_to :category1, :class_name => "Category", :foreign_key => "category1_id"
          belongs_to :category2, :class_name => "Category", :foreign_key => "category2_id"
        end
    
        class Category < ActiveRecord::Base
        end
    

    You can now use @customer.category1.name.

    (edited: belongs_to, not has_one)
    (edited: added :foreign_key)

    However, I think you are modeling a “many-to-many” relationship between customers and categories, right? Customers have multiple categories, and categories can be assigned to multiple customers. Take a look at has_and_belongs_to_many in ActiveRecord (see the guide: http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association).

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

Sidebar

Related Questions

I have an excel file with having a table (Customer Detail) with three columns.
Im having a databse with some customer information (customer_nr(primary key), name, address and so
I am having this Linq To SQL query which is taking Customer Category from
I am having New Customer page wherein the user fills information abourt customers ,
I'm working on a SaaS project that will have each customer having an instance
We keep having some customers getting a warning from AVG Antivirus about our application.
I'm having trouble coming up with a query that will find all customers who
I am trying to test the use-case of a customer having a proxy with
I'm defining a database for a customer/ order system where there are two highly
I'm having issues getting the customer.xml layout file to work properly for the customer's

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.