# Adding conditions to a report

**URL:** <https://www.ruby-forum.com/t/adding-conditions-to-a-report/128643>\
**Category:** Rails\
**Created:** [February 9, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/adding-conditions-to-a-report/128643 "2008-02-09T20:10:56Z")\
**Posts on this page:** 2\
**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, 8:10pm UTC](https://www.ruby-forum.com/t/adding-conditions-to-a-report/128643/1 "2008-02-09T20:10:56Z")

</div>

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 ☹

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

---

<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, 9:18pm UTC](https://www.ruby-forum.com/t/adding-conditions-to-a-report/128643/2 "2008-02-09T21:18:31Z")

</div>

On Sat, 09 Feb 2008 19:09:31 +0000, Thufir wrote:

> 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
