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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:52:27+00:00 2026-05-24T07:52:27+00:00

I’m trying to build an inventory system for the various servers and apps we

  • 0

I’m trying to build an inventory system for the various servers and apps we have. I have the following tables/Models:

  • App (Application)
  • Server (Server)
  • Environment (Working environment)

and then I also have a join table which holds the information for the server and apps, called “AppServer”.

Here is a picture of the ER Diagram (Sorry, I’m new so I can’t post pictures yet)

ER Diagram

The dotted lines between App->Environment and Server->Environment are indirect connections, i.e. going through AppServer.
Not the best ER Diagram but I tried to make it clear.

So now the ruby code I created to connect the various tables is as follows:

App Model:

class App < ActiveRecord::Base
  has_and_belongs_to_many :servers
  has_one :app_server
  has_one :environment, :through => :app_servers
end

Server Model:

class Server < ActiveRecord::Base
  has_and_belongs_to_many :apps
  has_one :app_server
  has_one :environment, :through => :app_servers
end

Environment Model:

class Environment < ActiveRecord::Base
  belongs_to :app_server
end

AppServer Model:

class AppServer < ActiveRecord::Base
  has_one :environment
  belongs_to :server
  belongs_to :app
end

Everything seems ok so far, I can access the ‘AppServer’ model from ‘App’ and from ‘Server’ by using this command in the ruby IRB console:

exampleApp.app_server
exampleServer.app_server

irb(main):183:0> exampleApp.app_server
=> #<AppServer app_id: 1, server_id: 1, environment_id: nil>
irb(main):184:0> exampleServer.app_server
=> #<AppServer app_id: 1, server_id: 1, environment_id: nil>

The problem is that if I try to see a list of servers or apps for instance by doing

irb(main):188:0> exampleServer.apps

I get the following error

irb(main):188:0> exampleServer.apps
ActiveRecord::StatementInvalid: Could not find table 'apps_servers'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter
.rb:295:in `table_structure'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter
.rb:186:in `columns'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/reflection.rb:230:in `columns'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/associations/has_and_belongs_to_ma
ny_association.rb:23:in `columns'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/associations/has_and_belongs_to_ma
ny_association.rb:9:in `initialize'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/associations.rb:1483:in `new'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/associations.rb:1483:in `block in
collection_reader_method'
    from (irb):188
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties3.0.9/lib/rails/commands.rb:23:in   `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Same thing when I try out

irb(main):189:0> exampleApp.servers

Funny thing is, I already have a table called ‘app_servers’, not ‘apps_servers’, and even when I tried that out I got other errors.
Anyways, now trying out whether the relationship works, I’ll let the code speak for itself:

irb(main):193:0> exampleServer.app_server.app
=> #<App id: 1, name: "Test App", description: "This is a test applicaiton", status: "Not deployed", created_at: "2011-0
8-05 09:47:53", updated_at: "2011-08-05 09:47:53">
irb(main):194:0> exampleServer.app_server.server
=> #<Server id: 1, name: "Test Server", locationID: nil, osID: nil, type: nil, comments: "This is just a testing server", 
costCentreID: nil, serverStartDate: nil, serverDecommissionDate: nil, created_at: "2011-08-05 09:50:40", updated_at: "
2011-08-05 09:50:40">

So that seems fine, however doing the following will give me a nil answer:

irb(main):192:0> exampleServer.app_server.environment
=> nil
irb(main):196:0> exampleApp.app_server.environment
=> nil

So trying to link it to an environment model called ‘exampleEnvironment’ which was instantiated, is also of no use, as the code will show

irb(main):006:0> exampleApp.app_server.environment << exampleEnvironment
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<
        from (irb):6
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

If you guys need any more info (Which I doubt from all this long text[Sorry (^^)]), let me know.

So my questions are, is my relationship wrong or is there something missing from my code?
All I want to be able to do is say

exampleServer.apps #Answer will show the linked app
exampleApp.servers #Answer will show the linked server
exampleServer.environment #Answer will show the linked environment through app_server
or
exampleServer.app_Server.environment #Answer will show the linked environment

Your help is greatly appreciated, and apologies for this looooooong question.
Thanks in advance d(-_-)b

  • 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-24T07:52:28+00:00Added an answer on May 24, 2026 at 7:52 am

    I may have misinterpreted your ER diagram, but it seems that Applications, Servers, and Environments are all independent entities, and an AppServer is a unique combination of one each of the three. The following is based on that assumption.

    In this case, AppServer is the join model for the other three models. This relationship in Rails is usually modeled like this:

    class App < ActiveRecord::Base
      has_many :app_servers
      has_many :servers, :through => :app_servers
      has_many :environments, :through => :app_servers
    end
    
    class Server < ActiveRecord::Base
      has_many :app_servers
      has_many :apps, :through => :app_servers
      has_many :environments, :through => :app_servers
    end
    
    class Environment < ActiveRecord::Base
      has_many :app_servers
      has_many :apps, :through => :app_servers
      has_many :servers, :through => :app_servers
    end
    
    class AppServer < ActiveRecord::Base
      belongs_to :app
      belongs_to :server
      belongs_to :environment
    end
    

    This should allow you to do all the queries you mentioned in your question.

    A good way to decide what kind of relationships to have between your models is to look at which tables will hold the foreign key. Since AppServer holds a app_id, for example, it belongs_to and App. An App therefore has_many or has_one AppServer.

    Another thing to note is that has_many :through is almost always preferred to has_and_belongs_to_many. It allows you to explicitly define the join model and make it as flexible as you’d like. In this case, you needed a join model for three tables, so the has_and_belongs_to_many default of two tables wouldn’t meet your needs.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.