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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:55:41+00:00 2026-05-14T05:55:41+00:00

I have two models: The model NetworkObject try to describe hosts. I want to

  • 0

I have two models:

The model NetworkObject try to describe “hosts”. I want to have a rule with source and destination, so i’m trying to use both objects from the same class since it dont makes sense to create two different classes.

class NetworkObject < ActiveRecord::Base
  attr_accessible :ip, :netmask, :name
  has_many :statements
  has_many :rules, :through =>:statements
end

class Rule < ActiveRecord::Base
  attr_accessible :active, :destination_ids, :source_ids
  has_many :statements
  has_many :sources, :through=> :statements, :source=> :network_object
  has_many :destinations, :through => :statements, :source=>  :network_object
end

To build the HABTM i did choose the Model JOIN. so in this case i created a model named Statement with:

class Statement < ActiveRecord::Base
  attr_accessible :source_id, :rule_id, :destination_id
  belongs_to  :network_object, :foreign_key => :source_id
  belongs_to :network_object,  :foreign_key => :destination_id
  belongs_to :rule
end

The problem is: is right to add two belongs_to to the same class using different foreign_keys? I tried all combinations like:

belongs_to :sources, :class_name => :network_object, :foreign_key => :source_id

but no success.. anything that i am doing wrong?

  • 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-14T05:55:41+00:00Added an answer on May 14, 2026 at 5:55 am

    The associations also need to know what foreign key to use. Try updating it to this. I have not tried this so let me know if it works or not.

    class Rule < ActiveRecord::Base
      attr_accessible :active, :destination_ids, :source_ids
      has_many :statements
      has_many :sources, :through => :statements, :class_name => "NetworkObject", :foreign_key => "source_id"
      has_many :destinations, :through => :statements, :class_name => "NetworkObject", :foreign_key => "destination_id"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that
Suppose you have two models, User and City, joined by a third model CityPermission:
I have two models indexed for searching (User and Item). I'm trying to do
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have two models with slug fields: class Book(models.Model): name = models.CharField(max_length=200) slug =
I have two models in my Django 1.1.1 application: class UserRequest(models.Model): # blah blah
I have two models: Play and PlayParticipant , defined (in part) as: class PlayParticipant(models.Model):
If I have two models in Django: class Blog(models.Model): author = models.CharField() class Post(models.Model):

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.