Hiding a column

Subscribe to Hiding a column 5 posts, 3 voices

 
Avatar Sam 26 posts

Totally noob question….say one has a standard scaffold generated list action of a specific model (like Task). How does one hide a column in the controller when getting the data? I’ve got it working in the view, which was fun and learned a few things, but that does not seem like a wise approach to me. It is not better to get data from the database that you don’t need. I hope this makes sense…if you need more info, let me know.

Thanks, Sam

 
Avatar Jeff Cohen 89 posts

If it’s just for a read-only view, use the :select option when doing your find. That lets you specify which columns should be returned, instead of all columns.

Beware, you won’t have access to another attributes, even though your Ruby model will let you try to get to them. So it’s kind of a bug waiting to happen when you forget that you had resitricted the column set.

I’d say, just don’t show it in the view, and optimize later. Probably, there are other parts of your code that will be slower and be worth optimizing, than worrying about getting fewer columns back from the database.

 
Avatar Sam 26 posts

My list action has the following code from the scaffold in it:

@task_pages, @tasks = paginate :tasks, :order => ‘position DESC’, :per_page => 10

I added the :order argument to the call, which obviously just works! But, I’m unclear what that line does. I can tell pagination is involved with the paginate :tasks. I’ve looked at the docs on railsbrain, but I’m still a bit unclear what that line does. Please enlighten me.

Thanks again, Sam

 
Avatar Brian Eng 49 posts

The paginate method (which is going to be deprecated, if I’m not mistaken) is much like a find, except it returns a two-element array of a Paginator and a collection of the records for the current page. It works by automatically providing a LIMIT and OFFSET to the SQL query that is created.

See here for more: http://api.rubyonrails.org/classes/ActionController/Pagination.html#M000130

 
Avatar Sam 26 posts

Thanks again guys…I think I’m starting to see the light!!! :). Once you actually start playing with Rails, you start to discover how fun it really is.