Hi, Is there a way to include associations of the associations you''re including? As an example, you could have "orders" belonging to an "account", which in turn belongs to a "person". When I want to list orders sorted by the name of the account owners, I need to be able to do something like: orders.find(:all, :include => "account.person", :order => "person.name") but that doesn''t work... I tried to do it like: orders.find(:all, :joins => "JOIN accounts AS account ON account.id = order.account_id JOIN people AS person ON person.id = account.person_id", :from => "order AS order") But that had the side effect of orders.id becoming the id of person. So, is there any good way of doing this? Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ 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 Thu, Mar 15, 2007 at 06:36:26AM -0700, halfgaar wrote :> Is there a way to include associations of the associations you''re > including? As an example, you could have "orders" belonging to an "account", which in turn belongs to a "person". When I want to list orders sorted by the name of the account ownersFor example: User.find_by_id(id, :include => [:accounts => [:orders => [:objects]]]) etc. Hope that helps, -- ,========================. | Pierre-Alexandre Meyer | | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | `========================'' --~--~---------~--~----~------------~-------~--~----~ 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 Mar 15, 2:45 pm, Pierre-Alexandre Meyer <p...-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org> wrote:> > For example: > > User.find_by_id(id, :include => [:accounts => [:orders => [:objects]]]) > > etc. > > Hope that helps,OK, I fixed it by doing this: Order.find(:all, :include => [:account => [:owner]], :order => "people.name") BTW, isn''t find_by_id very deprecated? --~--~---------~--~----~------------~-------~--~----~ 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 Thu, Mar 15, 2007 at 07:04:35AM -0700, halfgaar wrote :> BTW, isn''t find_by_id very deprecated?Deprecated???? Why? The point is, if you do something like: @user = User.find(5) you can crash your app if such a user doesn''t exist: ActiveRecord::RecordNotFound: Couldn''t find User with ID=5 But if you write: @user = User.find_by_id(5) you''ll have a nil object if it doesn''t exist. It''s so easier to handle: unless @user flash[:error] = ''Hey! you don\''t exist!'' end etc. My $0.02 -- ,========================. | Pierre-Alexandre Meyer | | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | `========================'' --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
But even easier than hitting that shift key twice for find_by_id is @user = User.find(5) rescue nil For me, anyhow. RSL On 3/15/07, Pierre-Alexandre Meyer <pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org> wrote:> > > On Thu, Mar 15, 2007 at 07:04:35AM -0700, halfgaar wrote : > > BTW, isn''t find_by_id very deprecated? > > Deprecated???? Why? > > The point is, if you do something like: > @user = User.find(5) > > you can crash your app if such a user doesn''t exist: > ActiveRecord::RecordNotFound: Couldn''t find User with ID=5 > > But if you write: > @user = User.find_by_id(5) > > you''ll have a nil object if it doesn''t exist. It''s so easier to handle: > > unless @user > flash[:error] = ''Hey! you don\''t exist!'' > end > > etc. > > > My $0.02 > > -- > ,========================. > | Pierre-Alexandre Meyer | > | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | > `========================'' > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Thu, Mar 15, 2007 at 10:26:10AM -0400, Russell Norris wrote :> But even easier than hitting that shift key twice for find_by_id is > > @user = User.find(5) rescue nilHéhéhé, AZERTY keyboard rocks! Anyway, do you put rescue everywhere in your code? -- ,========================. | Pierre-Alexandre Meyer | | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | `========================'' --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Everywhere? No. But I do use it fairly often when I'm hitting up AR for a record that may or may not exist. Why'd you ask? RSL On 3/15/07, Pierre-Alexandre Meyer <pam@mouraf.org> wrote:> > > On Thu, Mar 15, 2007 at 10:26:10AM -0400, Russell Norris wrote : > > But even easier than hitting that shift key twice for find_by_id is > > > > @user = User.find(5) rescue nil > > Héhéhé, AZERTY keyboard rocks! > > Anyway, do you put rescue everywhere in your code? > > -- > ,========================. > | Pierre-Alexandre Meyer | > | email : pam@mouraf.org | > `========================' > > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---
On Thu, Mar 15, 2007 at 10:42:07AM -0400, Russell Norris wrote :> Everywhere? No. But I do use it fairly often when I''m hitting up AR for a > record that may or may not exist. Why''d you ask?Just for curiosity. I have lots of find_by_something in my app to so I wanted to know how other developers are managing errors. -- ,========================. | Pierre-Alexandre Meyer | | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | `========================'' --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pierre-Alexandre Meyer wrote:> On Thu, Mar 15, 2007 at 10:42:07AM -0400, Russell Norris wrote : >> Everywhere? No. But I do use it fairly often when I''m hitting up AR for a >> record that may or may not exist. Why''d you ask? > > Just for curiosity. I have lots of find_by_something in my app to so > I wanted to know how other developers are managing errors. > > -- > ,========================. > | Pierre-Alexandre Meyer | > | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | > `========================''I''m using find_by_blah too. I was just about to ask about including "associations of associations" when I saw this thread. Talk about good timing :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
My preference for the find rescue nil methodology is just because I learned it first and I hate the shift key. That''s why I type with brackets where properly I should use parentheses. :/ I like the idea that find_by_id returns nil and does the same thing as the rescue though. That''s cool. But seriously rescue is your friend. I''ve got some code that renames thumbnails when the appropriate models attributes change and code like FileUtils.rm("#{path}/#{dir}/#{id}.#{extension}") rescue nil are a lot easier than checking if the file exists then deleting it. And code like def attachments_for(entry) return "(none)" if entry.attachments.empty? return "(file)" unless entry.has_image begin attachment = Attachment.find(entry.has_image) link_to(image_tag(attachment.image(:square), :alt => "image"), {:action => "edit", :id => entry.id}, :title => "Edit this entry?") rescue "(<em>error!</em>)" end end helps me generate thumbnails on the fly [if they don''t exist] and have a visible representation when something went wrong but not break the app. RSL On 3/15/07, Pierre-Alexandre Meyer <pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org> wrote:> > > On Thu, Mar 15, 2007 at 10:42:07AM -0400, Russell Norris wrote : > > Everywhere? No. But I do use it fairly often when I''m hitting up AR for > a > > record that may or may not exist. Why''d you ask? > > Just for curiosity. I have lots of find_by_something in my app to so > I wanted to know how other developers are managing errors. > > -- > ,========================. > | Pierre-Alexandre Meyer | > | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | > `========================'' > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Thu, Mar 15, 2007 at 11:10:07AM -0400, Russell Norris wrote :> But seriously rescue is your friend.I agree! To validate date format, I use in my model: date.propriete = Date.new(value.split(''/'')[2].to_i,value.split(''/'')[1].to_i,value.split(''/'')[0].to_i).strftime ''%d/%m/%Y''.to_s rescue date.propriete If the user type a wrong date in the in_place_editor_field, it won''t be saved. I just found weird to use the rescue for find method :) -- ,========================. | Pierre-Alexandre Meyer | | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | `========================'' --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I thought find rescue nil was normal and the way the tutorials do it. I dunno. Maybe I''m just weird. Well, I _know_ I''m weird. RSL On 3/15/07, Pierre-Alexandre Meyer <pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org> wrote:> > > On Thu, Mar 15, 2007 at 11:10:07AM -0400, Russell Norris wrote : > > But seriously rescue is your friend. > > I agree! > To validate date format, I use in my model: > > date.propriete = Date.new(value.split(''/'')[2].to_i,value.split > (''/'')[1].to_i,value.split(''/'')[0].to_i).strftime ''%d/%m/%Y''.to_s rescue > date.propriete > > If the user type a wrong date in the in_place_editor_field, it won''t be > saved. > > I just found weird to use the rescue for find method :) > > -- > ,========================. > | Pierre-Alexandre Meyer | > | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | > `========================'' > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Thu, Mar 15, 2007 at 11:23:15AM -0400, Russell Norris wrote :> I thought find rescue nil was normal and the way the tutorials do it. I > dunno. Maybe I''m just weird. Well, I _know_ I''m weird.:) Hey! May be I''m wrong too! Nobody has provided this method yet:>> begin?> User.find(55)>> rescue ActiveRecord::RecordNotFound >> nil >> ensure?> puts ''Oops, something goes really wrong''>> endOops, something goes really wrong => nil (so easy to put it in application.rb) -- ,========================. | Pierre-Alexandre Meyer | | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | `========================'' --~--~---------~--~----~------------~-------~--~----~ 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 Mar 15, 3:22 pm, Pierre-Alexandre Meyer <p...-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org> wrote:> > BTW, isn''t find_by_id very deprecated? > > Deprecated???? Why?Oh, wait. I was confusing it with find_all... find_by_id is simply part of the other find_by_<attribute>methods, of course... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---