Forum: Ruby on Rails Migrations - adding a new table and automatically creating r

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-15 07:16
(Received via mailing list)
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
1d8edfb3fcce3579c878ca8b06f77285?d=identicon&s=25 Christian (Guest)
on 2006-04-15 13:20
(Received via mailing list)
On Fri, Apr 14, 2006 at 10:13:27PM -0700, Craig White wrote:
>       t.column "name", :string
>   def self.down
>
You forgot to save them.
A0ed1bbfe42f4f87e6db0a16706246e2?d=identicon&s=25 mgreenly (Guest)
on 2006-04-15 16:22
Craig White 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 => "..."
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-05-03 18:56
(Received via mailing list)
use Role.create instead, as Role.new doesn't save the record
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-08-03 14:22
(Received via mailing list)
On Fri, 2006-04-14 at 23:16 -0600, Pat Maddox wrote:
> use Role.create instead, as Role.new doesn't save the record
>
>
> On 4/14/06, Craig White <craigwhite@azapple.com> 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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-08-03 14:22
(Received via mailing list)
On 4/15/06, Craig White <craigwhite@azapple.com> wrote:
>
>
>
> Rails@lists.rubyonrails.org
> 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) }
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-08-03 14:22
(Received via mailing list)
On Sat, 2006-04-15 at 01:50 -0600, Pat Maddox 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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-08-03 14:22
(Received via mailing list)
On Apr 15, 2006, at 7:25 AM, Craig White 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 Mornini
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-08-03 14:22
(Received via mailing list)
On Sat, 2006-04-15 at 10:23 -0700, Tom Mornini 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
This topic is locked and can not be replied to.