Forum: Ruby on Rails :include associations of associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Wiebe C. (Guest)
on 2007-03-15 15:37
(Received via mailing list)
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.
Pierre-Alexandre M. (Guest)
on 2007-03-15 15:47
(Received via mailing list)
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 
owners

For example:

User.find_by_id(id, :include => [:accounts => [:orders => [:objects]]])

etc.

Hope that helps,

--
,========================.
| Pierre-Alexandre M. |
| email : removed_email_address@domain.invalid |
`========================'
Wiebe C. (Guest)
on 2007-03-15 16:05
(Received via mailing list)
On Mar 15, 2:45 pm, Pierre-Alexandre M. <removed_email_address@domain.invalid> 
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?
Pierre-Alexandre M. (Guest)
on 2007-03-15 16:23
(Received via mailing list)
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 M. |
| email : removed_email_address@domain.invalid |
`========================'
Russell N. (Guest)
on 2007-03-15 16:28
(Received via mailing list)
But even easier than hitting that shift key twice for find_by_id is

@user = User.find(5) rescue nil

For me, anyhow.

RSL
Pierre-Alexandre M. (Guest)
on 2007-03-15 16:39
(Received via mailing list)
On Thu, Mar 15, 2007 at 10:26:10AM -0400, Russell N. 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 M. |
| email : removed_email_address@domain.invalid |
`========================'
Russell N. (Guest)
on 2007-03-15 16:45
(Received via mailing list)
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
Pierre-Alexandre M. (Guest)
on 2007-03-15 16:51
(Received via mailing list)
On Thu, Mar 15, 2007 at 10:42:07AM -0400, Russell N. 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 M. |
| email : removed_email_address@domain.invalid |
`========================'
Joe P. (Guest)
on 2007-03-15 16:56
Pierre-Alexandre M. wrote:
> On Thu, Mar 15, 2007 at 10:42:07AM -0400, Russell N. 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 M. |
> | email : removed_email_address@domain.invalid |
> `========================'

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 :)
Russell N. (Guest)
on 2007-03-15 17:10
(Received via mailing list)
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
Pierre-Alexandre M. (Guest)
on 2007-03-15 17:21
(Received via mailing list)
On Thu, Mar 15, 2007 at 11:10:07AM -0400, Russell N. 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 M. |
| email : removed_email_address@domain.invalid |
`========================'
Russell N. (Guest)
on 2007-03-15 17:24
(Received via mailing list)
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
Pierre-Alexandre M. (Guest)
on 2007-03-15 17:44
(Received via mailing list)
On Thu, Mar 15, 2007 at 11:23:15AM -0400, Russell N. 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'
>> end
Oops, something goes really wrong
=> nil

(so easy to put it in application.rb)

--
,========================.
| Pierre-Alexandre M. |
| email : removed_email_address@domain.invalid |
`========================'
Wiebe C. (Guest)
on 2007-03-15 18:11
(Received via mailing list)
On Mar 15, 3:22 pm, Pierre-Alexandre M. <removed_email_address@domain.invalid> 
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...
This topic is locked and can not be replied to.