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
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.
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 => “…”
use Role.create instead, as Role.new doesn’t save the record
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. [email protected] 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
On 4/15/06, Craig W. [email protected] wrote:
[email protected]
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) }
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.
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
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