?
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
Medium Priority
?
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
wesgarrison earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Smartwatches: just a fashion accessory or a useful device for all? The Apple Watch (http://www.apple.com/watch/) was launched in April of 2015 and has become a new way for iPhone users to stay connected. Ranging from $349 to $17,000, the Apple Watch…
Does your iMac really need a hardware upgrade? Will upgrading RAM speed-up your computer? If yes, then how can you proceed? Upgrading RAM in your iMac is not as simple as it may seem. This article will help you in getting and installing right RA…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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