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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:32:40+00:00 2026-05-18T05:32:40+00:00

Hi have a table named category which has two foreign keys to the table

  • 0

Hi have a table named category which has two foreign keys to the table user, structure is given below. How can I get the user name corresponding to created_by and modified_by by using relations. @category = Category.find(params[:id]) is giving only the details from the category table. My current model class is

class Category < ActiveRecord::Base
  validates_uniqueness_of :title, :message => "Title already exist"
  validates_presence_of :title, :message => "Cannot be blank"
  belongs_to :user
end

How can I associate both the fields created_by and modified_by to the user model

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `created_by` int(11) unsigned default NULL,
  `modified_by` int(11) unsigned default NULL,
  `created` datetime NOT NULL,
  `modified` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `created_by` (`created_by`),
  UNIQUE KEY `modified_by` (`modified_by`)

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `username` varchar(25) NOT NULL,
  `password` varchar(255) NOT NULL,
  `usertype_id` int(11) unsigned NOT NULL,
  `active` tinyint(1) NOT NULL,
  `created` datetime NOT NULL,
  `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `usertype_id` (`usertype_id`)
)


--
-- Constraints for table `categories`
--
ALTER TABLE `categories`
  ADD CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
  ADD CONSTRAINT `categories_ibfk_2` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;
  • 1 1 Answer
  • 1 View
  • 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-18T05:32:41+00:00Added an answer on May 18, 2026 at 5:32 am

    This should work for you:

    class Category < ActiveRecord::Base
      # ...
      validates_uniqueness_of :title, :message => "Title already exist"
      validates_presence_of :title, :message => "Cannot be blank"
      belongs_to :creator, :foreign_key => 'created_by', :class_name => 'User'
      belongs_to :editor, :foreign_key => 'modified_by', :class_name => 'User' 
      # ...
    end
    
    class User < ActiveRecord::Base
      # ...
      has_many :created_categories, :class_name => 'Category', :foreign_key => 'created_by'                                                                                
      has_many :modified_categories, :class_name => 'Category', :foreign_key => 'modified_by'
      # ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables: Category table which has an ID column, a description
I have a Category table which has 3 fields i'm interested in: ID, Name,
Let's say I have a table, category , which has 3 columns, id ,
I have a task table, which has a sub category. The sub category is
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the
I need to perform an ON DELETE CASCADE on my table named CATEGORY, which
I have two tables, one with categories and subcategories. Each category and subcategory has
I have a query which has a join on another table: select * from
Which is your preference? Let's say we have a generic Product table that has
I have a table, generated from a LINQ query on a datatable, which has

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.