A view of two models

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

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

thanks,

Thufir

This screencast might help

On Feb 8, 3:11 pm, DyingToLearn [email protected] wrote:

This screencast might help

#22 Eager Loading - RailsCasts

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

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

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

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

-Thufir

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