# Migrations - adding a new table and automatically creating r

**URL:** <https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868>\
**Category:** Rails\
**Created:** [April 15, 2006, 7:16am UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868 "2006-04-15T07:16:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 15, 2006, 7:16am UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/1 "2006-04-15T07:16:34Z")

</div>

I want to create table called roles and then populate it with some new  
records…This doesn’t work. Is there something I’m missing?

Craig

class AddRightsAndRolesTables \< ActiveRecord::Migration

def self.up

```
create_table :roles do |t|
  t.column "name", :string
end

  Role.reset_column_information
  Role.new :name => "Users Admin"
  Role.new :name => "Users Create"
  Role.new :name => "Users View"
  Role.new :name => "Users None"

```

end

def self.down  
drop\_table :roles  
end

end

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 15, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/2 "2006-04-15T13:20:04Z")

</div>

On Fri, Apr 14, 2006 at 10:13:27PM -0700, Craig W. wrote:

> ```
> t.column "name", :string
> 
> ```
> 
> def self.down

You forgot to save them.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 15, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/3 "2006-04-15T16:22:33Z")

</div>

Craig W. wrote:

> I want to create table called roles and then populate it with some new  
> records…This doesn’t work. Is there something I’m missing?
> 
> Craig
> 
> class AddRightsAndRolesTables \< ActiveRecord::Migration
> 
> def self.up
> 
> ```
> create_table :roles do |t|
> t.column "name", :string
> end
> 
> Role.reset_column_information
> Role.new :name => "Users Admin"
> Role.new :name => "Users Create"
> Role.new :name => "Users View"
> Role.new :name => "Users None"
> 
> ```
> 
> end
> 
> def self.down  
> drop\_table :roles  
> end
> 
> end

Instead of new use create

Role.create :name =\> “…”

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 3, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/4 "2006-05-03T18:56:20Z")

</div>

use Role.create instead, as Role.new doesn’t save the record

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [August 3, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/5 "2006-08-03T14:22:03Z")

</div>

On Fri, 2006-04-14 at 23:16 -0600, Pat M. wrote:

> use Role.create instead, as Role.new doesn’t save the record
> 
> On 4/14/06, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I want to create table called roles and then populate it with some new  
> > records…This doesn’t work. Is there something I’m missing?

* * *

bingo thanks…ok, one follow up question then…

another section of my migrations creates a join table…

```
create_table :rights_roles, :id => false do |t|
  t.column "right_id", :integer
  t.column "role_id", :integer
end

```

and that’s fine, but I am adding data to rights table and to roles table  
but the join table isn’t getting populated. So I figured I would do it  
afterwards this way…

```
  Right.find(:all).each do |ri|
    ro = Role.find(:first, :conditions => :name == ri.name)
    RightsRoles.create :right_id => ri, :role_id => ro
  end

```

but when I run the migration, I get the following error…

rake aborted!  
PGError: ERROR: relation “rights\_roles\_id\_seq” does not exist  
: SELECT currval(‘rights\_roles\_id\_seq’)

and of course, the relation “rights\_roles\_id\_seq” doesn’t exist because  
I told it not to create it above.

How do I populate a join table (or do I?) when I set values to the 2  
tables being joined in migration?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [August 3, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/6 "2006-08-03T14:22:47Z")

</div>

On 4/15/06, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I don’t see why you’d have a RightsRoles AR model anyway…

What you should do is  
Right.find(:all).each { |ri| ri.roles \<\< Role.find(:first,  
:conditions =\> :name == ri.name) }

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [August 3, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/7 "2006-08-03T14:22:47Z")

</div>

On Apr 15, 2006, at 7:25 AM, Craig W. wrote:

> > > Role.find(:first, :conditions =\> :name == “Case Managers Admin”)  
> > > =\> nil
> 
> Can someone clarify for me?

Role.find(:first, :conditions =\> “name = Case Managers Admin”)

:conditions is just a plain SQL string, or an array of query string  
and replacement values:

type = ‘Case Managers Admin’

Role.find(:first, :conditions =\> [“name = ?”,type])

placeholders are nice, because they will get quoted automatically,  
avoiding SQL injection. Eventually, no doubt, someone will wire  
up the correct functions in the DB driver and they’ll also be more  
efficient than interpolation (if SQL injection avoidance isn’t  
reason enough!).

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [August 3, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/8 "2006-08-03T14:22:48Z")

</div>

On Sat, 2006-04-15 at 10:23 -0700, Tom M. wrote:

> > 
> 
> type = ‘Case Managers Admin’
> 
> Role.find(:first, :conditions =\> [“name = ?”,type])
> 
> placeholders are nice, because they will get quoted automatically,  
> avoiding SQL injection. Eventually, no doubt, someone will wire  
> up the correct functions in the DB driver and they’ll also be more  
> efficient than interpolation (if SQL injection avoidance isn’t  
> reason enough!).

* * *

seeing as how this is for migrations, if I have to worry about sql  
injection here, I have a lot of issues to deal with.

Yes, it turned out that I only needed to simplify like the above.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [August 3, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/migrations-adding-a-new-table-and-automatically-creating-r/57868/9 "2006-08-03T14:22:47Z")

</div>

On Sat, 2006-04-15 at 01:50 -0600, Pat M. wrote:

> > bingo thanks…ok, one follow up question then…  
> > afterwards this way…  
> > : SELECT currval(‘rights\_roles\_id\_seq’)  
> > What you should do is  
> > Right.find(:all).each { |ri| ri.roles \<\< Role.find(:first,  
> > :conditions =\> :name == ri.name) }

* * *

makes sense but I’m getting unexpected behavior…so I’m trying to work  
it through via script/console…

This works…

> > Role.find(:first)  
> > =\> #\<Role:0xb7a1f048 @attributes={“name”=\>“Case Managers Admin”,  
> > “id”=\>“1”}\>

This doesn’t work

> > Role.find(:first, :conditions =\> :name == “Case Managers Admin”)  
> > =\> nil

Can someone clarify for me?

Thanks

Craig
