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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 1234From 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This screencast might help http://railscasts.com/episodes/22 On Feb 8, 3:02 pm, Thufir <hawat.thu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 8, 3:11 pm, DyingToLearn <phy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This screencast might help > > http://railscasts.com/episodes/22I 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/)
<table>
<tr>
<th>call_comment</th>
<th>login</th>
</tr>
<% for call in @calls %>
<tr>
<td><%= call.comment %></td>
<td><%= call.login.login %></td>
</tr>
<% end %>
<table>
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
On Feb 8, 3:43 pm, Thufir
<hawat.thu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Feb 8, 3:11 pm, DyingToLearn
<phy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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?
>
> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---