Ryan
2011-Feb-22 03:40 UTC
Beginner Help - Iterating and Displaying From Models with Relationship
This should probably be easy but for some reason I can''t get past a simple routine. I have a model for Companies and for Applications. A company has many applications. So in this instance I have a company id and I want to find all applications associated with that company. I tried the following: <%= Company.find(1).applications.each { |app| app.name } %> in my view. This returns "#<Application:0xb6736afc>#<Application: 0xb672f1d0>#<Application:0xb672f130>#<Application: 0xb672ef50>#<Application:0xb672ed34>#<Application: 0xb672e924>#<Application:0xb672e884>" when the view is rendered. I''m not really sure what this is, but it does have one entry for each of my applications for that company. So I know it is creating the right number of entries just not sure what the output means. I''m sure I am doing something monumentally stupid but I can''t seem to find any examples of this that return this output. Any help that can be provided is very much appreciated. Thank you! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Feb-22 07:54 UTC
Re: Beginner Help - Iterating and Displaying From Models with Relationship
On 22 Feb 2011, at 03:40, Ryan <ryandb2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This should probably be easy but for some reason I can''t get past a > simple routine. I have a model for Companies and for Applications. A > company has many applications. So in this instance I have a company > id and I want to find all applications associated with that company. > I tried the following: > > <%= Company.find(1).applications.each { |app| app.name } %> in my > view. >each returns the collection you iterated over. If you want the return value to be the array containing the result of evaluating the block for each object, then use map. Alternatively you can do <% collection.each do |object| %> <%= object.name %> <% end %> Fred> This returns "#<Application:0xb6736afc>#<Application: > 0xb672f1d0>#<Application:0xb672f130>#<Application: > 0xb672ef50>#<Application:0xb672ed34>#<Application: > 0xb672e924>#<Application:0xb672e884>" when the view is rendered. > > I''m not really sure what this is, but it does have one entry for each > of my applications for that company. So I know it is creating the > right number of entries just not sure what the output means. > > I''m sure I am doing something monumentally stupid but I can''t seem to > find any examples of this that return this output. Any help that can > be provided is very much appreciated. > > Thank you! > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Clint
2011-Feb-22 08:31 UTC
Re: Beginner Help - Iterating and Displaying From Models with Relationship
I am not at my computer, so I can''t help out with code. Basically you need to dig one layer deeper. The above is an array of Applications. Try to <%=raise Company.find(1).inspect %> to display the returned value/array. From there you can play around till it satisfy your needs. On Feb 22, 4:40 am, Ryan <ryan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This should probably be easy but for some reason I can''t get past a > simple routine. I have a model for Companies and for Applications. A > company has many applications. So in this instance I have a company > id and I want to find all applications associated with that company. > I tried the following: > > <%= Company.find(1).applications.each { |app| app.name } %> in my > view. > > This returns "#<Application:0xb6736afc>#<Application: > 0xb672f1d0>#<Application:0xb672f130>#<Application: > 0xb672ef50>#<Application:0xb672ed34>#<Application: > 0xb672e924>#<Application:0xb672e884>" when the view is rendered. > > I''m not really sure what this is, but it does have one entry for each > of my applications for that company. So I know it is creating the > right number of entries just not sure what the output means. > > I''m sure I am doing something monumentally stupid but I can''t seem to > find any examples of this that return this output. Any help that can > be provided is very much appreciated. > > Thank you!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bryan Crossland
2011-Feb-22 16:52 UTC
Re: Re: Beginner Help - Iterating and Displaying From Models with Relationship
Hi Ryan, Your loop is correct. The issue how erb is interpreting what you are asking it to do. The = is telling erb to spit out the contest of the array Company.find(1).applications.each without running the block it has. There are two ways to get it to display what you want. Remove the = and change your block to read { |app| puts app.name } or do as Fredrick suggested and write you code like so: <% Company.find(1).applications.each do |app| %> <%= app.name %> <% end %> Thanks, B. On Tue, Feb 22, 2011 at 2:31 AM, Clint <simon.krollpfeifer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am not at my computer, so I can''t help out with code. > Basically you need to dig one layer deeper. The above is an > array of Applications. Try to <%=raise Company.find(1).inspect %> to > display > the returned value/array. From there you can play around till it > satisfy your needs. > > On Feb 22, 4:40 am, Ryan <ryan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> This should probably be easy but for some reason I can''t get past a >> simple routine. I have a model for Companies and for Applications. A >> company has many applications. So in this instance I have a company >> id and I want to find all applications associated with that company. >> I tried the following: >> >> <%= Company.find(1).applications.each { |app| app.name } %> in my >> view. >> >> This returns "#<Application:0xb6736afc>#<Application: >> 0xb672f1d0>#<Application:0xb672f130>#<Application: >> 0xb672ef50>#<Application:0xb672ed34>#<Application: >> 0xb672e924>#<Application:0xb672e884>" when the view is rendered. >> >> I''m not really sure what this is, but it does have one entry for each >> of my applications for that company. So I know it is creating the >> right number of entries just not sure what the output means. >> >> I''m sure I am doing something monumentally stupid but I can''t seem to >> find any examples of this that return this output. Any help that can >> be provided is very much appreciated. >> >> Thank you! > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Feb-22 20:44 UTC
Re: Re: Beginner Help - Iterating and Displaying From Models with Relationship
On 22 February 2011 16:52, Bryan Crossland <bacrossland-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Ryan,Hiya, Can you not top-post, please. It makes it hard to know what you''re exactly replying to. TIA.>There are two ways to get it to display what you want. Remove the > = and change your block to read { |app| puts app.name } or do asThat''s not gonna do much good, as "puts" is going to output on the server console. The one way to do it is as Fred said. Michael -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ryan
2011-Feb-23 03:54 UTC
Re: Beginner Help - Iterating and Displaying From Models with Relationship
Thank you very much everyone! Fred, your advise was spot on. Worked like a charm! <% Company.find(1).applications.each do |app| %> <%= app.name %> <% end %> Perfecto! Clint, thanks for the tip on the inspect function, I missed that. It probably would have been the best way to debug and trial/error the solution. Once again thank you all for the help :) On Feb 22, 1:44 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 February 2011 16:52, Bryan Crossland <bacrossl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi Ryan, > > Hiya, > Can you not top-post, please. It makes it hard to know what you''re > exactly replying to. TIA. > > >There are two ways to get it to display what you want. Remove the > > = and change your block to read { |app| puts app.name } or do as > > That''s not gonna do much good, as "puts" is going to output on the > server console. The one way to do it is as Fred said. > > Michael-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.