:include associations of associations


#1

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.


#2

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 |
`========================’


#3

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?


#4

But even easier than hitting that shift key twice for find_by_id is

@user = User.find(5) rescue nil

For me, anyhow.

RSL


#5

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 |
`========================’


#6

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 |
`========================’


#7

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


#8

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 |
`========================’


#9

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 :slight_smile:


#10

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. :confused: 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
“(error!)”
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


#11

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 :slight_smile:


,========================.
| Pierre-Alexandre M. |
| email : removed_email_address@domain.invalid |
`========================’


#12

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


#13

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.

:slight_smile:

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 |
`========================’


#14

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_methods, of course…