adding conditions to a report

I think that I need to flesh out the Call.report method, or perhaps break it down, because it's not giving the desired output.

The array calls_logins has the condition in which the @call.login_id => 1, and this seems key to getting correct results. Wheras, c.report is more of a general report. Hmm, I do want the general report, but would also want to filter based on @Login.login as well.

I want to put this condition into @call.report so that only matching data is gathered. Hmm, seems like login_id won't really work for that :frowning:

Any pointers as how to change @call.report?

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:0xb705e968 @login=#<Login:0xb705e404 @attributes= {"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes= {"id"=>"1", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb705e260 @login=#<Login:0xb705e404 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"2", "comment"=>"start call", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb705dfcc @login=#<Login:0xb705e404 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"3", "comment"=>"start break", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>]

calls_logins.each {|event| puts event.login.login + "\t" +

event.created_at.to_s + "\t" + ?> event.comment} 0123 Fri Feb 08 15:12:13 -0800 2008 start work 0123 Fri Feb 08 15:12:13 -0800 2008 start call 0123 Fri Feb 08 15:12:13 -0800 2008 start break => [#<Call:0xb705e968 @login=#<Login:0xb705e404 @attributes= {"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes= {"id"=>"1", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb705e260 @login=#<Login:0xb705e404 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"2", "comment"=>"start call", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb705dfcc @login=#<Login:0xb705e404 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"3", "comment"=>"start break", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>]

c=Call.find_by_id(1)

=> #<Call:0xb70505ac @attributes={"id"=>"1", "comment"=>"start work", "login_id"=>"1", "created_at"=>"2008-02-08 15:12:13"}>

c.report.each {|event| puts event.login.login + "\t" +

event.created_at.to_s + "\t" + event.comment} 0123 Fri Feb 08 15:12:13 -0800 2008 start work 0123 Fri Feb 08 15:12:13 -0800 2008 start call 0123 Fri Feb 08 15:12:13 -0800 2008 start break 1234 Fri Feb 08 15:12:13 -0800 2008 start work => [#<Call:0xb7045e7c @login=#<Login:0xb7045cb0 @attributes= {"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes= {"id"=>"1", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb7045b48 @login=#<Login:0xb7045cb0 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"2", "comment"=>"start call", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb70458b4 @login=#<Login:0xb7045cb0 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"3", "comment"=>"start break", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb7045620 @login=#<Login:0xb7045454 @attributes={"employee_id"=>"1", "id"=>"2", "login"=>"1234"}>, @attributes={"id"=>"4", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"2"}>]

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 => [:login, :login])         end

end thufir@arrakis ~/goodfellow-tool $

thanks,

Thufir

I want to put this condition into @call.report so that only matching data is gathered. Hmm, seems like login_id won't really work for that

Any pointers as how to change @call.report?

Well, I narrowed down the problem, but my lack of ruby knowledge is hindering me.

Basically, I want the output of c.report, below, to match either report_one or report_two, depending on the value of c.login_id.

If c.login_id is 1, then the output of report_one is desired. If c.login_id is 2, then the output of report_two is desired.

Of course, this needs to be calculated dynamically.

  def report                 Call.find(:all, :include => [:login, :login], :conditions       => {:login_id => :login.id}).each {|event| puts     event.login.login + "\t" + event.created_at.to_s + "\t" +       event.comment}   end

Something like the above? I want to pull only matching Login data...

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

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

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

report_one.each {|event| puts event.login.login + "\t" +

event.created_at.to_s + "\t" + event.comment} 0123 Fri Feb 08 15:12:13 -0800 2008 start work 0123 Fri Feb 08 15:12:13 -0800 2008 start call 0123 Fri Feb 08 15:12:13 -0800 2008 start break => [#<Call:0xb701b488 @login=#<Login:0xb701af24 @attributes= {"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes= {"id"=>"1", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb701ad80 @login=#<Login:0xb701af24 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"2", "comment"=>"start call", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb701aaec @login=#<Login:0xb701af24 @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"3", "comment"=>"start break", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>]

report_two=Call.find(:all, :conditions => {:login_id => 2}, :include

=> :login) => [#<Call:0xb7008680 @login=#<Login:0xb70084b4 @attributes= {"employee_id"=>"1", "id"=>"2", "login"=>"1234"}>, @attributes= {"id"=>"4", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"2"}>]

report_two.each {|event| puts event.login.login + "\t" +

event.created_at.to_s + "\t" + event.comment} 1234 Fri Feb 08 15:12:13 -0800 2008 start work => [#<Call:0xb7008680 @login=#<Login:0xb70084b4 @attributes= {"employee_id"=>"1", "id"=>"2", "login"=>"1234"}>, @attributes= {"id"=>"4", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"2"}>]

c=Call.find_by_id(4)

=> #<Call:0xb6ffe57c @attributes={"id"=>"4", "comment"=>"start work", "login_id"=>"2", "created_at"=>"2008-02-08 15:12:13"}>

c.report

0123 Fri Feb 08 15:12:13 -0800 2008 start work 0123 Fri Feb 08 15:12:13 -0800 2008 start call 0123 Fri Feb 08 15:12:13 -0800 2008 start break => [#<Call:0xb6ffa008 @login=#<Login:0xb6ff9e3c @attributes= {"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes= {"id"=>"1", "comment"=>"start work", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb6ff9cd4 @login=#<Login:0xb6ff9e3c @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"2", "comment"=>"start call", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"1"}>, #<Call:0xb6ff9a40 @login=#<Login:0xb6ff9e3c @attributes={"employee_id"=>"1", "id"=>"1", "login"=>"0123"}>, @attributes={"id"=>"3", "comment"=>"start break", "created_at"=>"2008-02-08 15:12:13", "login_id"=>"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 => [:login, :login], :conditions => {:login_id => 1}).each {|event| puts event.login.login + "\t" + event.created_at.to_s + "\t" + event.comment}         end

end thufir@arrakis ~/goodfellow-tool $

thanks,

Thufir