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.
mysql> CREATE TABLE many_columns (c1 text, c2 text, c3 text, c4 text);
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO many_columns (c1,c2,c3,c4) VALUES ('t1', 't2', 't3',
't4');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM many_columns;
+------+------+------+------+
| c1 | c2 | c3 | c4 |
+------+------+------+------+
| t1 | t2 | t3 | t4 |
+------+------+------+------+
1 row in set (0.00 sec)
mysql> CREATE VIEW less_columns AS SELECT c1, c2 FROM many_columns;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * from less_columns;
+------+------+
| c1 | c2 |
+------+------+
| t1 | t2 |
+------+------+
1 row in set (0.00 sec)
mysql> INSERT INTO less_columns (c1,c2) VALUES ('tl1', 'tl2');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * from less_columns;
+------+------+
| c1 | c2 |
+------+------+
| t1 | t2 |
| tl1 | tl2 |
+------+------+
2 rows in set (0.00 sec)
mysql> SELECT * from many_columns;
+------+------+------+------+
| c1 | c2 | c3 | c4 |
+------+------+------+------+
| t1 | t2 | t3 | t4 |
| tl1 | tl2 | NULL | NULL |
+------+------+------+------+
2 rows in set (0.00 sec)
HTH,
Peter
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.