Thufir
#1
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:
[email protected] ~/goodfellow-tool $
[email protected] ~/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
[email protected] ~/goodfellow-tool $
[email protected] ~/goodfellow-tool $
I would like
Thufir
#2
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
Thufir
#3
Thufir
#4
On Feb 8, 3:11 pm, DyingToLearn [email protected] wrote:
This screencast might help
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?
[email protected] ~/goodfellow-tool $
[email protected] ~/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
[email protected] ~/goodfellow-tool $ rails --version
Rails 1.2.5
[email protected] ~/goodfellow-tool $
thanks,
Thufir
Thufir
#5
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
Thufir
#6
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/)
call_comment |
login |
<% for call in @calls %>
<%= call.comment %> |
<%= call.login.login %> |
<% end %>
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