Craig White
2006-Apr-15 05:13 UTC
[Rails] Migrations - adding a new table and automatically creating records
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
Pat Maddox
2006-Apr-15 05:16 UTC
[Rails] Migrations - adding a new table and automatically creating records
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? > > 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Craig White
2006-Apr-15 06:48 UTC
[Rails] Migrations - adding a new table and automatically creating records
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
Pat Maddox
2006-Apr-15 07:50 UTC
[Rails] Migrations - adding a new table and automatically creating records
On 4/15/06, Craig White <craigwhite@azapple.com> wrote:> 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 > > _______________________________________________ > Rails mailing list > 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) }
Christian
2006-Apr-15 11:18 UTC
[Rails] Re: Migrations - adding a new table and automatically creating records
On Fri, Apr 14, 2006 at 10:13:27PM -0700, 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >You forgot to save them. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: Digital signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060415/c27894c6/attachment.bin
mgreenly
2006-Apr-15 14:22 UTC
[Rails] Re: Migrations - adding a new table and automatically creati
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 > > endInstead of new use create Role.create :name => "..." -- Posted via http://www.ruby-forum.com/.
Craig White
2006-Apr-15 14:25 UTC
[Rails] Migrations - adding a new table and automatically creating records
On Sat, 2006-04-15 at 01:50 -0600, Pat Maddox wrote:> On 4/15/06, Craig White <craigwhite@azapple.com> wrote: > > 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? > > > > 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) }---- 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
Tom Mornini
2006-Apr-15 17:23 UTC
[Rails] Migrations - adding a new table and automatically creating records
On Apr 15, 2006, at 7:25 AM, Craig White wrote:> 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?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
Craig White
2006-Apr-15 20:23 UTC
[Rails] Migrations - adding a new table and automatically creating records
On Sat, 2006-04-15 at 10:23 -0700, Tom Mornini wrote:> On Apr 15, 2006, at 7:25 AM, Craig White wrote: > > > 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? > > 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!).---- 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