# A view of two models

**URL:** <https://www.ruby-forum.com/t/a-view-of-two-models/128577>\
**Category:** Rails\
**Created:** [February 9, 2008, 12:03am UTC](https://www.ruby-forum.com/t/a-view-of-two-models/128577 "2008-02-09T00:03:23Z")\
**Posts on this page:** 6\
**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, 12:03am UTC](https://www.ruby-forum.com/t/a-view-of-two-models/128577/1 "2008-02-09T00:03:23Z")

</div>

I have two models which are related by a foreign key. I would like to  
have a report along the lines of:

# call\_comment login

first 1234

so that it prints fields calls.comment and logins.login where  
calls.login\_id=logins.id

(my SQL isn’t quite up to snuff for the above query, I keep getting a  
syntax error.)

I can kinda do that as below:

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

> >

?\> Call.find\_by\_id(1)  
=\> #\<Call:0xb6fc108c @attributes={“id”=\>“1”, “comment”=\>“first”,  
“login\_id”=\>“2”, “created\_at”=\>“2008-02-08 13:37:29”}\>

> >

?\> Login.find\_by\_id(2)  
=\> #\<Login:0xb6fb13f8 @attributes={“id”=\>“2”, “employee\_id”=\>“1”,  
“login”=\>“1234”}\>

> >

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

I would like

---

<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, 12:11am UTC](https://www.ruby-forum.com/t/a-view-of-two-models/128577/2 "2008-02-09T00:11:36Z")

</div>

On Fri, 08 Feb 2008 23:02:33 +0000, Thufir wrote:

> I have two models which are related by a foreign key. I would like to  
> have a report along the lines of:
> 
> # call\_comment login
> 
> first 1234

From sqlite, it looks like so:

sqlite\>  
sqlite\> SELECT calls.comment,logins.login FROM calls,logins WHERE  
calls.login\_id=logins.id;  
first|1234  
second|0123  
first|1234  
sqlite\>

But, of course, I’d like to do that from Ruby on Rails 🙂

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, 12:12am UTC](https://www.ruby-forum.com/t/a-view-of-two-models/128577/3 "2008-02-09T00:12:11Z")

</div>

This screencast might help

> **[\#22 Eager Loading - RailsCasts](http://railscasts.com/episodes/22-eager-loading)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<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, 12:43am UTC](https://www.ruby-forum.com/t/a-view-of-two-models/128577/4 "2008-02-09T00:43:52Z")

</div>

On Feb 8, 3:11 pm, DyingToLearn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This screencast might help
> 
> [#22 Eager Loading - RailsCasts](http://railscasts.com/episodes/22)

I get sound for mp3’s, but not for that screencast ☹

It’s helpful, but my controller looks quite different from his  
(probably deprecated?). In my controller, I want something like:

@calls=Call.find(:all, :include =\> [:logins, :login])

Is that right? Do I create a new method?

thufir@arrakis ~/goodfellow-tool $  
thufir@arrakis ~/goodfellow-tool $ cat -n app/controllers/  
calls\_controller.rb | head -n 5 | tail -n 4  
2 def index  
3 list  
4 render :action =\> ‘list’  
5 end  
thufir@arrakis ~/goodfellow-tool $ rails --version  
Rails 1.2.5  
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 20, 2008, 11:40am UTC](https://www.ruby-forum.com/t/a-view-of-two-models/128577/5 "2008-02-20T11:40:42Z")

</div>

On Sun, 17 Feb 2008 13:31:44 -0800, DyingToLearn wrote:

> if you want to reduce the number of database calls, change this  
> @calls = Call.find(:all)  
> to this  
> @calls = Call.find(:all, :include =\> :login)
> 
> doing that is not necessary though.

perhaps not, but it’s good to know different ways of doing things!

I believe that I have the models/relationships configured correctly, but  
I haven’t had a chance to play with rails in a while ☹

My gentoo system is all screwed up, I can’t get ruby working ☹

-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 17, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/a-view-of-two-models/128577/6 "2008-02-17T22:32:32Z")

</div>

Do you have  
belongs\_to :login  
In you Call model?

if so, then you should be able to do this in your CallController  
def index  
@calls = Call.find(:all)  
end

and this in your view (index.rhtml or index.html.erb under app/views/  
call/)

 \<% for call in @calls %\> \<% end %\>

| call\_comment | login |
| --- | --- |
| \<%= call.comment %\> | \<%= call.login.login %\> |

if you want to reduce the number of database calls, change this  
@calls = Call.find(:all)  
to this  
@calls = Call.find(:all, :include =\> :login)

doing that is not necessary though.

Hope that helps
