# How to create a Database Table in a Controller

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-database-table-in-a-controller/92265>\
**Category:** Rails\
**Created:** [April 17, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/how-to-create-a-database-table-in-a-controller/92265 "2007-04-17T17:50:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 17, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/how-to-create-a-database-table-in-a-controller/92265/1 "2007-04-17T17:50:05Z")

</div>

I need to create a database table in a controller method, but I get a  
undefined method `create\_table’ error. so, how to to mixin the methods  
that will be needed for creating a table?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 17, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/how-to-create-a-database-table-in-a-controller/92265/2 "2007-04-17T21:45:21Z")

</div>

Hello!

Unfortunately, you won’t be able to use a mixin to access the methods  
that you want to actually create the database table. The create\_table  
method can only be accessed through classes that inherit  
ActiveRecord::Migration.

I would suggest creating another class that inherits  
ActiveRecord::Migration and then setup some methods to set all of the  
attributes of the table. Once you’ve set that up then you can call  
the migrate method of the class and it will update the database.

# In controller

class TableController \< ApplicationController  
def create  
@results = CreateUsers.migrate(:up)  
end

class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table …  
end

```
def self.down
  drop_table ....
end

```

end  
end

Now if you’re managing your database using migrations, I would not  
recommend updating the database inside the controller unless you save  
the migration to the filesystem and manage the versioning on the  
schema\_info table.

Hope this helps.

On Apr 17, 10:50 am, Nanyang Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 17, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/how-to-create-a-database-table-in-a-controller/92265/3 "2007-04-17T22:42:20Z")

</div>

rimas.silkaitis wrote:

> # In controller
> 
> class TableController \< ApplicationController  
> def create  
> @results = CreateUsers.migrate(:up)  
> end

what does “CreateUsers.migrate(:up)” mean?

I want to create a table, and the columns of this table will depend on a  
hash. This hash includes column name and column type information. Can I  
pass this hash to the CreateUsers.migrate(:up) method?

> class CreateUsers \< ActiveRecord::Migration  
> def self.up  
> create\_table …  
> end  
> …

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 17, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-create-a-database-table-in-a-controller/92265/4 "2007-04-17T22:52:00Z")

</div>

I mean I don’t know where to put arguments for a method call like  
CreateUsers.migrate(:up).

and why call CreateUsers.migrate(:up), not CreateUsers.up?
