querying multiple tables from the console

So, I'm just trying to understand the console a bit better :slight_smile:

I'm able to get some decent output through the console, but it's a bit short of what I want, which is to put the "calls" array together with the "logins" array and output the results as the SQL below does, something like:

calls.comment calls.created_at logins.login

If the relationships are in place... calls=Call.find(:all, :conditions => {:login_id => 1},:include => :logins) calls.each {|call| puts call.comment + "\t" + call.created_at.to_s + "\t" + call.login.to_s}

w/o knowing more about the schema, that's all i can really help with

If the relationships are in place... calls=Call.find(:all, :conditions => {:login_id => 1},:include => :logins) calls.each {|call| puts call.comment + "\t" + call.created_at.to_s + "\t" + call.login.to_s}

w/o knowing more about the schema, that's all i can really help with

I believe that the relationships are in place and that the schema is compatible:

thufir@arrakis ~/goodfellow-tool $ thufir@arrakis ~/goodfellow-tool $ script/console
Loading development environment.

calls=Call.find(:all, :conditions => {:login_id => 1},:include

=> :logins) ActiveRecord::ConfigurationError: Association named 'logins' was not found; perhaps you misspelled it?         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1351:in `build'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1356:in `build'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1355:in `each'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1355:in `build'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1319:in `initialize'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1037:in `new'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1037:in `find_with_associations'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1036:in `catch'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations.rb:1036:in `find_with_associations'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:996:in `find_every'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:418:in `find'         from (irb):1

quit

thufir@arrakis ~/goodfellow-tool $ thufir@arrakis ~/goodfellow-tool $ cat app/models/call.rb class Call < ActiveRecord::Base         belongs_to :login

        def report                 Call.find(:all, :include => [:logins, :login])         end end thufir@arrakis ~/goodfellow-tool $ thufir@arrakis ~/goodfellow-tool $ cat app/models/login.rb class Login < ActiveRecord::Base         belongs_to :employee         has_many :call end thufir@arrakis ~/goodfellow-tool $ thufir@arrakis ~/goodfellow-tool $ cat db/migrate/001_calls.rb class Calls < ActiveRecord::Migration         def self.up                 create_table "calls" do |call|                         call.column "login_id", :string                         call.column "created_at", :datetime                         call.column "comment", :string                 end

                Call.create :login_id => "1",                                         :comment => "start work"

                Call.create :login_id => "1",                                         :comment => "start call"

                Call.create :login_id => "1",                                         :comment => "start break"

                Call.create :login_id => "2",                                         :comment => "start work"

        end

        def self.down                 drop_table "calls"         end end thufir@arrakis ~/goodfellow-tool $ thufir@arrakis ~/goodfellow-tool $ cat db/migrate/003_logins.rb
class Logins < ActiveRecord::Migration         def self.up                 create_table "logins" do |login|                         login.column "login", :string                         login.column "employee_id", :string                 end

                Login.create :login => "0123",                                         :employee_id => "1"

                Login.create :login => "1234",                                         :employee_id => "1"

                Login.create :login => "2345",                                         :employee_id => "2"         end

        def self.down                 drop_table "logins"         end end thufir@arrakis ~/goodfellow-tool $

thanks,

Thufir

I believe that the relationships are in place and that the schema is compatible:

Sorry, just a minor tweak and it's all good :slight_smile:

That's very cool, I'm still poking it, but that is very, very, interesting!

thufir@arrakis ~/goodfellow-tool $ thufir@arrakis ~/goodfellow-tool $ script/console Loading development environment.

calls_logins=Call.find(:all, :conditions => {:login_id => 1},:include

=> :login) => [#<Call:0xb703f93c @attributes={"id"=>"1", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}, @login=#<Login:0xb703f3d8 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>>, #<Call:0xb703f234 @attributes={"id"=>"2", "comment"=>"start call", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}, @login=#<Login:0xb703f3d8 @attributes= {"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>>, #<Call:0xb703efa0 @attributes={"id"=>"3", "comment"=>"start break", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}, @login=#<Login:0xb703f3d8 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>>]

?> calls_logins[1].comment => "start call"

?> calls_logins[1].login.login => "0123"

?> quit thufir@arrakis ~/goodfellow-tool $

thanks,

Thufir