Hello,

I've decided to use multiple tables for an entity (e.g. "todo_items1,"
"todo_items2," "todo_items3," etc.), instead of just one main table
which could end up having a lot of rows (e.g. just "todo_items"). I'm
doing this to try and to avoid a potential future performance drop that
could come with having too many rows in one table.

With that, I'm looking for a good way to handle this in Rails, mainly by
trying to avoid loading a bunch of unused associations for each User
object. I'm guessing that other have done something similar, so there's
probably a few good tips/recommendations out there.

(I know that I could use a partition for this, but, for now, I've
decided to go the 'multiple tables' route.)

Each user has their todo_items placed into a specific table. The user's
"todo items" table is set when the user is created, and all of their
todo_items go into the same table. The data in their todo items
collection is private, so when it comes time to process a users
todo_items, I'll only have to look at one table.

One thing I don't particularly want to have is a bunch of unused
associations in the User class. Right now, it looks like I'd have to do
the following:

class User < ActiveRecord::Base
  has_many :todo_items1, :todo_items2, :todo_items3, :todo_items4,
:todo_items5
end

class todo_items1 < ActiveRecord::Base
  belongs_to :user
end

class todo_items2 < ActiveRecord::Base
  belongs_to :user
end

class todo_items3 < ActiveRecord::Base
  belongs_to :user
end

The thing is, for each individual user, only one of the "todo items"
tables would be usable/applicable/accessible since all of a user's
todo_items are stored in the same table. This means only one of the
associations would be in use at any time and all of the other "has_many
:todo_itemsX" associations that were loaded would be a waste.

For example, with a user.id of 2, I'd only need
"todo_items3.find_by_text('search_word')", but the way I'm thinking of
setting this up (see above), I'd still have access to todo_items1,
todo_items2, todo_items4 and todo_items5.

I'm thinking that these "extra associations" adds extra overhead and
makes each User object's size in memory much bigger than it has to be.

Also, there's a bunch of stuff that Ruby/Rails is doing in the
background which may make this implementation. For example, I'm guessing
that there could be some additional method call/lookup overhead for each
User object, since it has to load all of those associations, which in
turn creates all of those nice, dynamic model accessor methods like
"User.find_by_something."

I don't really know Ruby/Rails does internally with all of those
has_many associations though, so maybe it's not so bad. But right now
I'm thinking that it's really wasteful, and that there may just be a
better, more efficient way of doing this.

So, a few questions:

1) Is there's some sort of special Ruby/Rails methodology that could be
applied to this 'multiple tables to represent one entity' scheme? Are
there any 'best practices' for this?

2) Is it really bad to have so many unused has_many associations for
each object? Is there a better way to do this?

3) Does anyone have any advice on how to abstract the fact that there's
multiple "todo items" tables behind a single todo_items model/class? For
example, so I can call "todo_items.find_by_text('search_phrase')"
instead of "todo_items3.find_by_text('search_phrase')," or even
"@user.todo_items?"

Thank you!
-- 
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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to