model fields

I have a database table with over 100 fields, however I want my model
to include only a few fields so that every time I do a query/update, it
will only do so for those fields.

Is there a way to specify in a model to only use selected fields from a
table and ignore the rest?

Thankyou.

Why do you want to do it?
You don't want to give possibility to update some fiends to some
users?
Or you have some fields with a lot of data(BLOB) ?

Simon Grant wrote in post #968977:

I have a database table with over 100 fields, however I want my model
to include only a few fields so that every time I do a query/update, it
will only do so for those fields.

Why? What's the point?

Is there a way to specify in a model to only use selected fields from a
table and ignore the rest?

Well, you could use the :select option, or (depending on your use case)
attr_protected might help. But I don't see why you're bothering.

Thankyou.

Best,

Simon Grant wrote in post #968977:

I have a database table with over 100 fields, however I want my model
to include only a few fields so that every time I do a query/update, it
will only do so for those fields.

Is there a way to specify in a model to only use selected fields from a
table and ignore the rest?

If you want hard protection for reading and writing only
to a few columns, you could use a VIEW. In that way,
you would also stay with the default Rails behaviour for
that limited set of columns.

CREATE TABLE many_columns (c1 text, c2 text, c3 text, c4 text);

Query OK, 0 rows affected (0.00 sec)

INSERT INTO many_columns (c1,c2,c3,c4) VALUES ('t1', 't2', 't3',

't4');
Query OK, 1 row affected (0.00 sec)

yurokle wrote in post #969023:

Why do you want to do it?
You don't want to give possibility to update some fiends to some
users?
Or you have some fields with a lot of data(BLOB) ?

I basically want to maximize efficiency. I don't see why I would be
reading and writing additionaly database fields if I don't have to,
especially for very large tables.

Say that I want to create a web form that contains only a few fields
from a table of 100 fields. To use a model from the table, I would have
to read all the fields of the model (through an ActiveModel::find ) to
populate the form. Then when I want to update the values of the form
back to the database, will not all the 100 field values of the model be
updated? or just the ones changed. Seems to be very inefficient. I only
want to read and write the values of the form that I need.

Marnen Laibow-Koser wrote in post #969070:

Please do not top post, it makes it difficult to follow the thread,
insert your comments into the previous post. Thanks

Say that I want to create a web form that contains only a few fields
from a table of 100 fields. To use a model from the table, I would have
to read all the fields of the model (through an ActiveModel::find ) to
populate the form. Then when I want to update the values of the form
back to the database, will not all the 100 field values of the model be
updated? or just the ones changed. Seems to be very inefficient. I only
want to read and write the values of the form that I need.

You may think that that there is efficiency but do you know that it
actually matters in your case? I think you are proposing what is
commonly known as Premature Optimisation.

In my experience the bottlenecks in an app are rarely where you expect
them to be at the start. Design your app initially in the simplest
possible way, simple code means less bugs and less development time.
Use Test Driven Design so that you have automated tests that show that
all is working. Once it is running then _if_ you run into throughput
problems that is the time to work out where the bottleneck is and
refactor to improve the situation. As I said above I can virtually
guarantee that you will find that the bottleneck is not where you
initially guessed it would be. The fact that you have a full test
suite means you can happily refactor secure in the knowledge that your
app continues to work.

Colin

Simon Grant wrote in post #969223:

Say that I want to create a web form that contains only a few fields
from a table of 100 fields. To use a model from the table, I would have
to read all the fields of the model (through an ActiveModel::find ) to
populate the form.

Wrong -- you can use the :select option.

Then when I want to update the values of the form
back to the database, will not all the 100 field values of the model be
updated? or just the ones changed.

Just the ones in the form.

When in doubt, watch the generated SQL.

Seems to be very inefficient. I only
want to read and write the values of the form that I need.

That's all that happens. And Colin is right: you're trying to
prematurely optimize. Don't do that.

Best,

Simon Grant wrote in post #969223:

Say that I want to create a web form that contains only a few fields
from a table of 100 fields. To use a model from the table, I would have
to read all the fields of the model (through an ActiveModel::find ) to
populate the form. Then when I want to update the values of the form
back to the database, will not all the 100 field values of the model be
updated? or just the ones changed. Seems to be very inefficient. I only
want to read and write the values of the form that I need.

It is often more efficient to reply below the already quoted text of the
discussion. So I will continue my reply further below.

Marnen Laibow-Koser wrote in post #969070:

Simon Grant wrote in post #968977:

I have a database table with over 100 fields, however I want my model
to include only a few fields so that every time I do a query/update, it
will only do so for those fields.

Why? What's the point?

Is there a way to specify in a model to only use selected fields from a
table and ignore the rest?

Well, you could use the :select option, or (depending on your use case)
attr_protected might help. But I don't see why you're bothering.

Sorry long post, covering 3 techniques.

1. SELECT:

Thanks to all who responded to my query. Pleasantly surprised as a
newbie at the active and passionate rails community.