Say I have a table with 3 columns:
id integer
name varchar(50)
data text
Is there anyway to have active record think that only the columns “id”
and “name” exist? Such that it never loads all the text in the “data”
column when fetching rows.
The idea is we have an external process that reads and write to the data
field directly (without active record) but don’t want to make rails have
to load this field every time for no reason.
Thanks.
use the :select option to find:
:select: By default, this is * as in SELECT * FROM, but can be changed
if you for example want to do a join, but not include the joined
columns
Mike
but is there a way to enforce this in the model somewhere so no one
accidentally calls it without remembering to use :select
Mike G. wrote:
use the :select option to find:
:select: By default, this is * as in SELECT * FROM, but can be changed
if you for example want to do a join, but not include the joined
columns
Mike
tmac wrote:
Override the find method in your model class… untested but perhaps
something like this:
def find(*args)
without_data do
super
end
end
Thanks, I got this to work:
class Foo < ActiveRecord::Base
set_table_name ‘foo’
SELECT_FIELDS = [‘name’]
def self.find(*args)
found_hash = false
args.each do |arg|
if arg.class == Hash
found_hash = true
arg[:select] = SELECT_FIELDS
end
end
if not found_hash
args << {:select => SELECT_FIELDS}
end
super(*args)
end
end
see any flaws in the logic?
It seems like what we want is a plug-in that does something like
:attr_ignore, which would then define the select fields by taking the
@attributes keys and subtracting the items from the :attr_ignore list.
Anyone know of such a plug-in?
Override the find method in your model class… untested but perhaps
something like this:
def find(*args)
without_data do
super
end
end
def without_data(&block)
with_scope(:find => { :select => [“name”] }, &block)
end
WARNING! I’ve never implemented anything quite like that so I have no
idea how close that code is to working but I think it’s the right
idea.
good luck!
On Jul 6, 11:48 am, Andrew A. [email protected]