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

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 :frowning:

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 $

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

On Fri, 08 Feb 2008 21:29:33 -0800,
[email protected] 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:inbuild’
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:inbuild’
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:innew’
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:incatch’
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:infind_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

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 :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