:include associations of associations

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.

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?

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

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

@user = User.find(5) rescue nil

For me, anyhow.

RSL

Héhéhé, AZERTY keyboard rocks!

Anyway, do you put rescue everywhere in your code?

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

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.

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

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:

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

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)

Oh, wait. I was confusing it with find_all... find_by_id is simply
part of the other find_by_<attribute>methods, of course...