# Active Records with column privilages

**URL:** <https://www.ruby-forum.com/t/active-records-with-column-privilages/165422>\
**Category:** Rails\
**Created:** [April 13, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/active-records-with-column-privilages/165422 "2009-04-13T21:23:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gordons11](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a88e4f/32.png) [@gordons11](https://www.ruby-forum.com/u/gordons11)\
**Post date:** [April 13, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/active-records-with-column-privilages/165422/1 "2009-04-13T21:23:42Z")

</div>

Hey I am making applications that has different DB roles to the same  
database. One is pretty much the admin access where the person can see  
all the columns in each table, then there is the regular joe DB role  
where I don’t let him see confidential information that only admin can  
see, for security purposes, so I put column privileges on that role. The  
problem with that is the fact that Rails do not see these privileges set  
by the DB and Rails gets all the columns in a table. I do not want it to  
do that. I wish to prevent those columns from showing in the model. Is  
this possible?

---

<div class="post-metadata">

**Author:** ![gordons11](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a88e4f/32.png) [@gordons11](https://www.ruby-forum.com/u/gordons11)\
**Post date:** [April 13, 2009, 10:38pm UTC](https://www.ruby-forum.com/t/active-records-with-column-privilages/165422/2 "2009-04-13T22:38:05Z")

</div>

nvm, i checked out ActiveRecord::Base and I found no such thing. But I  
did find a nice patch that was made 2 years ago  
[http://dev.rubyonrails.org/ticket/8913](http://dev.rubyonrails.org/ticket/8913)

I changed it around to this to match the the code in rails 2.2.2

```
   # Hide columns from active record
   def attr_hidden(*attributes)
     write_inheritable_attribute(:attr_hidden, 

```

Set.new(attributes.map(&:to\_s)) + (hidden\_attributes || []))  
end

```
   def hidden_attributes # :nodoc:
     read_inheritable_attribute(:attr_hidden)
   end

```

and changed ActiveRecord::Base.column ofcourse

```
  # Returns an array of column objects for the table associated with 

```

this class.  
def columns  
unless defined?(@columns) && @columns  
@columns = connection.columns(table\_name, “#{name}  
Columns”).delete\_if {|c| attr\_hidden.member?(c.name)}  
@columns.each { |column| column.primary = column.name ==  
primary\_key }  
end  
@columns  
end

wish something like this is implemented. in my mind it is pretty useful.  
Ofcourse i do wish it could be a lil bit more automated by looking into  
the DB for the column privileges, but this is cool. =D
