Solved

How do I find out the variable type of a certain field retrieved from the database in Rails / Ruby?

Posted on 2010-08-14
1
592 Views
Last Modified: 2013-11-24
Hi, I have an application in Ruby on Rails which I need to find out what the column type is in the database through active record.

So for example, if I have a model Contact with the fields :name string and :total integer. In the model, the defaults created_at for timestamp and id will be there.

I would like to know how to find out based on a post from a form, what the particular column is.

So ... I have a form which posts a variable field_name through to an action in a controller.

In the controller, I've got an action ...

def find_out

contact = Contact.find(:first)
field_name = params[:field_name]
# find out what the field_name's column type is in Contact model

if column_type(field_name) == 'timestamp'
# do something
elsif column_type(field_name) == 'integer'
# do something else
end

end

I hope you get the idea... it's like the mysql_field_type() in PHP.

Thanks.

end
0
Comment
Question by:binele
1 Comment
 
LVL 14

Accepted Solution

by:
wesgarrison earned 500 total points
ID: 33439044
You can use script/console to play around with these things, here's a sample from one of my apps:
# For example, typing "User"
> User

# returns the class with all fields and their types
 => User(id: integer, name: string, ...)

# you can just get the Ruby class:
> User.first.name.class
 => String 


# or, maybe you need the db definition:
> User.new.column_for_attribute('name')
 => #<ActiveRecord::ConnectionAdapters::MysqlColumn:0x20c2090 @default=nil, @limit=100, @type=:string, @null=true, @scale=nil, @name="middle", @sql_type="varchar(100)", @precision=nil, @primary=false>

# use the sql_type to get the actual type:
> User.new.column_for_attribute('middle').sql_type
 => "varchar(100)" 

# all of that to say, Ruby classes are probably what you're looking for:

Contact.first.attribute(params[:field]).class

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Apple iTunes - Move from PC to PC First login to your old PC 1. Click on File. 2. Then click Library. 3. Now open the Export Library. 4. Save the .xml file to your desktop. This file will hold all of the information about your playl…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now