[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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
?
615 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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

656 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