开发者

Accessing a table in Ruby on Rails via relationship

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", descri开发者_如何学JAVAption: "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


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.


This is for the first problem.

irb(main):188:0> exampleServer.apps
ActiveRecord::StatementInvalid: Could not find table 'apps_servers'

-You need an intermediate many-to-may table called 'apps_servers' (name is by convention).

-The table will contain two columns with names 'app_id'/integer, 'server_id'/integer. The values of these columns are the primary key value of app row, and server row that is to be linked.

Please lookup Rails guide on many-to-many associations (HABTM). http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

Hope it helps.


After checking your ERD, I think I know the problem.

Rails favors convention over configuration. So, when you tell Rails that your App model

has_one :app_server

,it is expected that your app_server table has app_id. In your ERD, I saw you had appID as the field name.

If you try changing the foreign keys in all the tables accordingly, setting, app_id, server_id, environment_id, etc. whenever necessary, I am sure that you will find everything working perfectly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜