I'm working on a large project that handles quite some AJAX and
database interactions in short periods of time.
I'll simplify the project to an example. Say we're building houses and
a housebuilding project consists of floors and rooms, then to load a
configurator to edit the room and floor structure we would want to
load a project, which contains a few floors and each floor will
contain a few rooms. I would want to load the floors in a project, but
also the rooms in the floor that has been set to active using the "a"
flag. The other rooms are loaded when another floor is activated.
The models are built in a way so that a project has_many floors and a
floor has_many rooms. A room belongs_to a floor and a floor belongs_to
a project.
But here I'm executing the Floor.find method twice and I feel that's
not very DNY. Now I can think of two things to do. The first one is to
get the line out of @floors where "a" is true, but I can only think of
a for loop to do that and I feel there must be a simpler way. Is
there?
The second way is a way in which I would use more of the model's
features. So load the project and all its children (floors and rooms).
But I cannot find any documentation on that. I want this done the
Rails way and I really have a feeling that I could easily cut the
number of database requests. Either way would be fine.
Okay, thanks. But this includes all the rooms in a project. In my
project that could mean up to 255 results while I only want five or
six. I only want the rooms in the floor with :a => true. How to
include this in this method?
And this descends alot more in my project. Say I want to have
furniture in one of the rooms. Is is as simple as adding @furniture =
@rooms.map { |r| r.furniture }.flatten ? Can I keep on descending this
way?
Okay, thanks. But this includes all the rooms in a project. In my
project that could mean up to 255 results while I only want five or
six. I only want the rooms in the floor with :a => true. How to
include this in this method?
And this descends alot more in my project. Say I want to have
furniture in one of the rooms. Is is as simple as adding @furniture =
@rooms.map { |r| r.furniture }.flatten ? Can I keep on descending this
way?
Yep. But you should read more about active_record associations and
eager loading.
Thanks, this got me on the right track. I won't be able to get all my
data in one database request, but at least I reduced the number of
database requests by two.
Yep. But you should read more about active_record associations and
eager loading.
You could have an association with the condition a=true and include
that association
I'm not sure what you mean. If you mean I should only get the floor(s)
with a=true: that's not what I want. I also want to list all floors in
the configure view. I just want to load the rooms from the floor where
a=true.
Thank you very much, Maurício Linhares. Thanks to your finger pointing
me towards eager loading I now understand alot more about the way
Rails models can be used in your controller. I can load my
configurator using just one database interaction now! My models are
getting fatter and fatter and my controller are getting thinner and
thinner.
You could easily have two associations: rooms and rooms_with_a. Eager
load rooms_with_a when you want those with a = true and use the other
association if not. Sounds like you actually want to load all the
rooms though, just pull out some of them separately, in which case you
could do
You could easily have two associations: rooms and rooms_with_a. Eager
load rooms_with_a when you want those with a = true and use the other
association if not. Sounds like you actually want to load all the
rooms though, just pull out some of them separately, in which case you
could do
Well, in @project you would get all the rooms from all the floors like
this. I don't want that. I really want to get only the things I need.
So what I did now is:
In the project model I added had_one :active_floor and in the floor
model I added has_one :active_room
And that does exactly what I want it to do. There may be errors in the
method above though, as I wanted to stick to the example and had to
change some things around.
I'd just like to play spoiler and remind you that optimizing for smallest number of queries is not the same as optimizing for execution speed. Doing everything the Rails way is absolutely the right thing to do, but crunching down your database accesses early in development by using Ruby is betting that a Ruby implementation of array folding will be faster than a correctly written SQL implementation. If your result set is small, then it won't matter. It you have tens of thousands of rows, get your SQL right. Just a thought.
I'd just like to play spoiler and remind you that optimizing for
smallest number of queries is not the same as optimizing for execution
speed. Doing everything the Rails way is absolutely the right thing to
do, but crunching down your database accesses early in development by
using Ruby is betting that a Ruby implementation of array folding will
be faster than a correctly written SQL implementation. If your result
set is small, then it won't matter. It you have tens of thousands of
rows, get your SQL right. Just a thought.
Thanks for your input.
I really feel stuffing Ruby with 255 rows (the application max) when
you only need five or six is not the way to go. Especially if that
request will probably be executed more than once every second. That's
why I brought this up right now, whem I'm still in the early stages of
development. Bare in mind that this, for me, is a learning process,
but at the end it will have to be done right. That's why your thoughts
on this are very valuable to me.
The reason why I don't want to do:
@project = Project.find(:params[:id])
@floors = @project.floors@rooms = @floors.rooms
.. is simply because I don't need @floors.rooms. I need
@active_floor.rooms, as I will request the rooms on other floors when
another floor is set to active. I do, however, need the names of every
floor in a project.
I feel making one database request for everything I need is better
than making five, but I've now learned find doesn't work that way.
@floors = @project.floors triggers another database request. But
still, this has helped me alot, because my code is alot cleaner now.
And it's more complicated than that: while your database server can
definitely do some things a lot quicker than fetching the rows in ruby
and then doing stuff to them, it is a lot easier to scale out your
ruby stuff (add more servers running your mongrels or passenger) than
it is to do the same thing to your database server.
And it's more complicated than that: while your database server can
definitely do some things a lot quicker than fetching the rows in ruby
and then doing stuff to them, it is a lot easier to scale out your
ruby stuff (add more servers running your mongrels or passenger) than
it is to do the same thing to your database server.
Fred
I'm not sure I agree with that. I'm coming from the hosting business
and you will generally have to use database clusters on large-scaled
applications or web sites anyway. It would be great if it was just
about adding a HDD, but that's only possible if your load stays below
the threshold. Of course on very large scaled applications you would
use caching servers and stuff like that, but that's not relevant here.
In my opinion you would rather have your database servers do the heavy
work as much as possible. A database server is designed to do that.
Just use your resources wisely.
> And it's more complicated than that: while your database server can
> definitely do some things a lot quicker than fetching the rows in ruby
> and then doing stuff to them, it is a lot easier to scale out your
> ruby stuff (add more servers running your mongrels or passenger) than
> it is to do the same thing to your database server.
> Fred
I'm not sure I agree with that. I'm coming from the hosting business
and you will generally have to use database clusters on large-scaled
applications or web sites anyway. It would be great if it was just
about adding a HDD, but that's only possible if your load stays below
the threshold.
Sure, you can set up multiple master stuff, shard your data, run
memcache servers, read from slaves etc. None of these are trivial
though, certainly less so in my experience than adding another server
running mongrels into your cluster (which requires 0 code changes).
Not saying either that doing everything in ruby is the right thing
either, just raising the point that it's not a black and white world.