more than one table per model

Hi everyone,

I want to create a model that uses two tables for its data. Is that
possible or just a bad idea? I don't want to do the has_one because I
want to avoid the extra dereferencing.

For example

Model A
  - attributes name in first table
  - attribute phone_num in second table

Thanks,
Frank

Frank Kim wrote:

Hi everyone,

I want to create a model that uses two tables for its data. Is that
possible or just a bad idea? I don't want to do the has_one because I
want to avoid the extra dereferencing.

I usually avoid doing this, but...
"I want to create a creature with two hands. Is that possible or a bad
idea? I don't want to link them to the same area of the brain because I
want to avoid putting five fingers on each hand".

I mean, maybe I'm completely off, but it sounds like you just want to
create an extra table. What possible benefit could you derive from this,
if the data won't be separate? I guess, if both your tables have 200
fields, you would eventually derive an increase in read speeds if you
don't need all the data.. But I can't think of another reason.

You _can_ do it. Sometimes it's just a really bad idea, but you can
always do it.

Okay here's why I want to do it.
The first table contains just data.
The second table will be a view whose data can change depending on
other external factors. It could change daily.
Yes this second table could be an association but I would have
preferred it not to be.

Frank Kim wrote:

Okay here's why I want to do it.
The first table contains just data.
The second table will be a view whose data can change depending on
other external factors. It could change daily.
Yes this second table could be an association but I would have
preferred it not to be.

Well, then it really is two separate things, and it should be two
separate things. You are right. Update the second table every so often
with a scheduled task.

A second table is required when you have constraints that you don't
want interfering with your ability to save data to the first table.
has_one is very useful for this scenario.

I can't tell what your angle is from this post - are you disagreeing
with Aldric, or the OP?

A has_one is the association the OP said he didn't want; and the idea
of a second table with no association is what Aldric seems to be
querying.

Frank Kim wrote:

Okay here's why I want to do it.
The first table contains just data.
The second table will be a view whose data can change depending on
other external factors. It could change daily.
Yes this second table could be an association but I would have
preferred it not to be.

I don't think ActiveRecord is going to be your friend here. AFAIK
ActiveRecord expects a model to represent a single database table.

For instance you can override the conventional mapping of a model to a
table using;

set_table_name "my_table_name"

However, I know of no way to set multiple table names to one model. You
can certainly use more than one model mapped to a single table (STI).
But, not the other way around (again AFAIK).

Beside this, much of the functionality of ActiveRecord would no longer
work. How would it know which tables are represented by the model, and
which attributes belongs to which table. It would not have the benefit
of a join in order to determine what goes where.

Just my 2 cents.

Okay here's why I want to do it.
The first table contains just data.
The second table will be a view whose data can change depending on
other external factors. It could change daily.
Yes this second table could be an association but I would have
preferred it not to be.

Why would you prefer it not to be? You are presumably going to have
some sort of key linking the two tables otherwise how do you know
which rows in the tables go together. In that case what overhead is
there to using two models? Rails should be able to build essentially
the same queries as you would do yourself.

Colin

Hi everyone,

I want to create a model that uses two tables for its data. Is that

possible or just a bad idea? I don’t want to do the has_one because I

want to avoid the extra dereferencing.

For example

Model A

  • attributes name in first table

  • attribute phone_num in second table

Thanks,

Frank

Frank, the only reason that I can see having multiple tables would be in the following scenario:

class User < AR

has_many :phone_numbers

end

class PhoneNumber

belongs_to :user

end

Now, both User and PhoneNumber would have there own individual database tables. Also, the phone_numbers table will require a foreign key (i.e. user_id). I would recommend reading the relevant sections on associations in “Agile Web Development with Rails 3rd” by Dave Thomas et al or consult the guides.rubyonrails.com.

Good luck,

-Conrad

Hi everyone,

I want to create a model that uses two tables for its data. Is that

possible or just a bad idea? I don’t want to do the has_one because I

want to avoid the extra dereferencing.

For example

Model A

  • attributes name in first table

  • attribute phone_num in second table

Thanks,

Frank

Frank, the only reason that I can see having multiple tables would be in the following scenario:

class User < AR

has_many :phone_numbers

end

class PhoneNumber

belongs_to :user

end

The above should be

class PhoneNumber < AR

belongs_to :user

end

Note: AR is a shorthand for ActiveRecord::Base.

-Conrad

Thanks everyone for all your insight. It was very helpful.

I was hoping to avoid dereferencing but I guess there's no way around
it. I will look into using has_one with a scheduled service for
updating the secondary table.

Thanks!