active record, ignore a column

42 views
Skip to first unread message

Andrew Arrow

unread,
Jul 6, 2007, 2:29:31 PM7/6/07
to rubyonra...@googlegroups.com
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.

--
Posted via http://www.ruby-forum.com/.

Mike Garey

unread,
Jul 6, 2007, 2:44:45 PM7/6/07
to rubyonra...@googlegroups.com
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

Andrew Arrow

unread,
Jul 6, 2007, 2:48:10 PM7/6/07
to rubyonra...@googlegroups.com
but is there a way to enforce this in the model somewhere so no one
accidentally calls it without remembering to use :select

Mike Garey 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

unread,
Jul 6, 2007, 3:00:25 PM7/6/07
to Ruby on Rails: Talk
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 Arrow <rails-mailing-l...@andreas-s.net>
wrote:

Andrew Arrow

unread,
Jul 9, 2007, 2:31:02 PM7/9/07
to rubyonra...@googlegroups.com
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?

Mike Stangel

unread,
Jul 9, 2007, 3:06:36 PM7/9/07
to rubyonra...@googlegroups.com
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?
Reply all
Reply to author
Forward
0 new messages