# Querying multiple tables from the console

**URL:** <https://www.ruby-forum.com/t/querying-multiple-tables-from-the-console/128601>\
**Category:** Rails\
**Created:** [February 9, 2008, 6:10am UTC](https://www.ruby-forum.com/t/querying-multiple-tables-from-the-console/128601 "2008-02-09T06:10:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 9, 2008, 6:10am UTC](https://www.ruby-forum.com/t/querying-multiple-tables-from-the-console/128601/1 "2008-02-09T06:10:20Z")

</div>

So, I’m just trying to understand the console a bit better 🙂

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

start work 2008-02-08 15:12:13 0123  
start call 2008-02-08 15:12:13 0123  
start break 2008-02-08 15:12:13 0123

How do you put it all together from the console? I can print out the  
first two columns together, or print the third column, but cannot put  
them together ☹

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

> > calls=Call.find(:all, :conditions =\> {:login\_id =\> 1})  
> > =\> [#\<Call:0xb70747b8 @attributes={“id”=\>“1”, “comment”=\>“start work”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>,  
> > #\<Call:0xb707477c  
> > @attributes={“id”=\>“2”, “comment”=\>“start call”, “login\_id”=\>“1”,  
> > “created\_at”=\>“2008-02-08 15:12:13”}\>, #\<Call:0xb7074754 @attributes=  
> > {“id”=\>“3”, “comment”=\>“start break”, “login\_id”=\>“1”,  
> > “created\_at”=\>“2008-02-08 15:12:13”}\>]
> 
> > calls.each {|call| puts call.comment + “\t” + call.created\_at.to\_s}  
> > start work Fri Feb 08 15:12:13 -0800 2008  
> > start call Fri Feb 08 15:12:13 -0800 2008  
> > start break Fri Feb 08 15:12:13 -0800 2008  
> > =\> [#\<Call:0xb70747b8 @attributes={“id”=\>“1”, “comment”=\>“start work”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>,  
> > #\<Call:0xb707477c  
> > @attributes={“id”=\>“2”, “comment”=\>“start call”, “login\_id”=\>“1”,  
> > “created\_at”=\>“2008-02-08 15:12:13”}\>, #\<Call:0xb7074754 @attributes=  
> > {“id”=\>“3”, “comment”=\>“start break”, “login\_id”=\>“1”,  
> > “created\_at”=\>“2008-02-08 15:12:13”}\>]
> 
> > Login.find :all  
> > =\> [#\<Login:0xb705c58c @attributes={“id”=\>“1”, “employee\_id”=\>“1”,  
> > “login”=\>“0123”}\>, #\<Login:0xb705c550 @attributes={“id”=\>“2”,  
> > “employee\_id”=\>“1”, “login”=\>“1234”}\>, #\<Login:0xb705c528 @attributes=  
> > {“id”=\>“3”, “employee\_id”=\>“2”, “login”=\>“2345”}\>]
> 
> > logins=Login.find(:all, :conditions =\> {:login =\> “0123”})  
> > =\> [#\<Login:0xb7057190 @attributes={“id”=\>“1”, “employee\_id”=\>“1”,  
> > “login”=\>“0123”}\>]
> 
> > logins.each {|login| puts login.login}  
> > 0123  
> > =\> [#\<Login:0xb7057190 @attributes={“id”=\>“1”, “employee\_id”=\>“1”,  
> > “login”=\>“0123”}\>]
> 
> > quit  
> > thufir@arrakis ~/goodfellow-tool $  
> > thufir@arrakis ~/goodfellow-tool $ sqlite3 db/development.sqlite3  
> > SQLite version 3.4.1  
> > Enter “.help” for instructions  
> > sqlite\> SELECT calls.comment,calls.created\_at,logins.login FROM  
> > calls,logins WHERE calls.login\_id=1 AND logins.id=1;  
> > start work|2008-02-08 15:12:13|0123  
> > start call|2008-02-08 15:12:13|0123  
> > start break|2008-02-08 15:12:13|0123  
> > sqlite\> .quit  
> > thufir@arrakis ~/goodfellow-tool $

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 9, 2008, 6:30am UTC](https://www.ruby-forum.com/t/querying-multiple-tables-from-the-console/128601/2 "2008-02-09T06:30:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 9, 2008, 6:40am UTC](https://www.ruby-forum.com/t/querying-multiple-tables-from-the-console/128601/3 "2008-02-09T06:40:48Z")

</div>

On Fri, 08 Feb 2008 21:29:33 -0800,  
removed\_email\_address@domain.invalid wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 9, 2008, 6:55am UTC](https://www.ruby-forum.com/t/querying-multiple-tables-from-the-console/128601/4 "2008-02-09T06:55:27Z")

</div>

On Sat, 09 Feb 2008 05:39:34 +0000, Thufir wrote:

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

Sorry, just a minor tweak and it’s all good 🙂

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
