Hi, so I''m new to rails development, and I''m following a book called Practical Rails: Social Networking Sites. There''s a user role system that the book develops, each user has a one or more roles and vice versa. So I have the tables users, roles and roles_users (which connects users to roles and vice versa). In the Role model I have has_and_belongs_to_many :users and in the User model I have has_and_belongs_to_many: roles. To get the joint table I ran the command: script/generate migration CreateRolesUsersJoin and in the 2010301_create_roles_users_join.rb file I have the following: def self.up create_table :roles_users, :id => false do |t| t.column :role_id, :integer, :null => false t.column :user_id, :integer, :null => false end admin_user = User.create(:username => ''Admin'', :email => ''ali-86qk6qiIKr0@public.gmane.org'', :profile => ''Site Administrator'', :pwd => ''admin'', :pwd_confirmation => ''admin'') admin_role = Role.find_by_name(''Administrator'') admin_user.roles << admin_role end end so after that I run db:migrate and I expect that a new user called Admin should be created int eh database and the roles_users database should have one record in it connecting user Admin and role Administrator... but my databases are empty. Any suggestion? I''m using rails 2.3.5 and I think the book uses an older version so I suspect something to do with a version difference, but not sure. Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 May 2010 06:32, Ali <ali.akhtarzada-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, so I''m new to rails development, and I''m following a book called > Practical Rails: Social Networking Sites. There''s a user role system > that the book develops, each user has a one or more roles and vice > versa. So I have the tables users, roles and roles_users (which > connects users to roles and vice versa). > > In the Role model I have has_and_belongs_to_many :users and in the > User model I have has_and_belongs_to_many: roles. > > To get the joint table I ran the command: script/generate migration > CreateRolesUsersJoin and in the 2010301_create_roles_users_join.rb > file I have the following: > > def self.up > create_table :roles_users, :id => false do |t| > t.column :role_id, :integer, :null => false > t.column :user_id, :integer, :null => false > end > admin_user = User.create(:username => ''Admin'', > :email => ''ali@ex.com'', > :profile => ''Site Administrator'', > :pwd => ''admin'', > :pwd_confirmation => ''admin'')What happens if you type this into the ruby console (ruby script/console)? Does it create a user? Perhaps the validations on User are failing.> > admin_role = Role.find_by_name(''Administrator'')The line above is looking for a Role ''Administrator'' in the roles table. Is there such a record in the roles table? Colin> admin_user.roles << admin_role > end > end > > so after that I run db:migrate and I expect that a new user called > Admin should be created int eh database and the roles_users database > should have one record in it connecting user Admin and role > Administrator... but my databases are empty. > > Any suggestion? I''m using rails 2.3.5 and I think the book uses an > older version so I suspect something to do with a version difference, > but not sure.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, ok so it''s fixed now and I have no idea how. I don''t know what the console output was the first time I ran the db:migrate command. I tried running it quite a few times after that and it wouldn''t output anything new. Anyway, I ran db:reset and everything work now. Just to answer the other questions, yes the Administrator role was being created properly and User validations were not failing. Something else was up I guess. Maybe join tables have to be created at the same time? I had the users table set up a while ago... Cheers, - Ali On Tue, May 18, 2010 at 8:17 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 May 2010 06:32, Ali <ali.akhtarzada-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, so I''m new to rails development, and I''m following a book called > > Practical Rails: Social Networking Sites. There''s a user role system > > that the book develops, each user has a one or more roles and vice > > versa. So I have the tables users, roles and roles_users (which > > connects users to roles and vice versa). > > > > In the Role model I have has_and_belongs_to_many :users and in the > > User model I have has_and_belongs_to_many: roles. > > > > To get the joint table I ran the command: script/generate migration > > CreateRolesUsersJoin and in the 2010301_create_roles_users_join.rb > > file I have the following: > > > > def self.up > > create_table :roles_users, :id => false do |t| > > t.column :role_id, :integer, :null => false > > t.column :user_id, :integer, :null => false > > end > > admin_user = User.create(:username => ''Admin'', > > :email => ''ali-86qk6qiIKr0@public.gmane.org'', > > :profile => ''Site Administrator'', > > :pwd => ''admin'', > > :pwd_confirmation => ''admin'') > > What happens if you type this into the ruby console (ruby > script/console)? Does it create a user? Perhaps the validations on > User are failing. > > > > > admin_role = Role.find_by_name(''Administrator'') > > The line above is looking for a Role ''Administrator'' in the roles > table. Is there such a record in the roles table? > > Colin > > > admin_user.roles << admin_role > > end > > end > > > > so after that I run db:migrate and I expect that a new user called > > Admin should be created int eh database and the roles_users database > > should have one record in it connecting user Admin and role > > Administrator... but my databases are empty. > > > > Any suggestion? I''m using rails 2.3.5 and I think the book uses an > > older version so I suspect something to do with a version difference, > > but not sure. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 18, 2010, at 1:32 AM, Ali wrote:> Hi, so I''m new to rails development, and I''m following a book called > Practical Rails: Social Networking Sites. There''s a user role system > that the book develops, each user has a one or more roles and vice > versa. So I have the tables users, roles and roles_users (which > connects users to roles and vice versa). > > In the Role model I have has_and_belongs_to_many :users and in the > User model I have has_and_belongs_to_many: roles. > > To get the joint table I ran the command: script/generate migration > CreateRolesUsersJoin and in the 2010301_create_roles_users_join.rb > file I have the following: >class CreateRolesUsersJoin < ActiveRecord::Migration class User < ActiveRecord::Base has_and_belongs_to_many: roles # include code as needed (like a before_save :encrypt_password) end class Role < ActiveRecord::Base has_and_belongs_to_many :users end> def self.up > create_table :roles_users, :id => false do |t| > t.column :role_id, :integer, :null => false > t.column :user_id, :integer, :null => false > endUser.reset_column_information Role.reset_column_information> admin_user = User.create(:username => ''Admin'', > :email => ''ali-86qk6qiIKr0@public.gmane.org'', > :profile => ''Site Administrator'', > :pwd => ''admin'', > :pwd_confirmation => ''admin'') > > admin_role = Role.find_by_name(''Administrator'') > admin_user.roles << admin_role > end > endend> > so after that I run db:migrate and I expect that a new user called > Admin should be created int eh database and the roles_users database > should have one record in it connecting user Admin and role > Administrator... but my databases are empty. > > Any suggestion? I''m using rails 2.3.5 and I think the book uses an > older version so I suspect something to do with a version difference, > but not sure. > > Thanks in advance.Before you use a Model inside a migration, it''s a good idea to create a special class for it *inside* the migration (which is why I added the class around your code). These models should have just enough content for the purpose of the migration. I''m not sure about the HABTM relationships, but I threw .reset_column_information calls in there for both models since the roles_users table is new and common to both. One question though, why didn''t you create the Admin user when the User table was created? Do you have logic to keep the last user with an ''Administrator'' role from being deleted? -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://gaslightsoftware.com rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Rob, That would make more sense creating the Administrator at the time of the users migration, but I''m following a book and the roles were created later so I suppose the author just figured he''d throw it in there or something. I do not have logic that prevents the admin from being deleted at this point either. Noted :) Thanks. Is it standard practice to have the classes (Role and User in this case) defined inside the migration RolesUsersJoin, wouldn''t it be inconvenient since I would have my Role and User model defined somewhere else anyway? Or are you saying I should not define a User and Role elsewhere but just have them inside the RolesUsersJoin class? - Ali On Wed, May 19, 2010 at 12:35 AM, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote:> On May 18, 2010, at 1:32 AM, Ali wrote: > > Hi, so I''m new to rails development, and I''m following a book called >> Practical Rails: Social Networking Sites. There''s a user role system >> that the book develops, each user has a one or more roles and vice >> versa. So I have the tables users, roles and roles_users (which >> connects users to roles and vice versa). >> >> In the Role model I have has_and_belongs_to_many :users and in the >> User model I have has_and_belongs_to_many: roles. >> >> To get the joint table I ran the command: script/generate migration >> CreateRolesUsersJoin and in the 2010301_create_roles_users_join.rb >> file I have the following: >> >> class CreateRolesUsersJoin < ActiveRecord::Migration > class User < ActiveRecord::Base > > has_and_belongs_to_many: roles > # include code as needed (like a before_save :encrypt_password) > end > class Role < ActiveRecord::Base > > has_and_belongs_to_many :users > end > > > def self.up >> create_table :roles_users, :id => false do |t| >> t.column :role_id, :integer, :null => false >> t.column :user_id, :integer, :null => false >> end >> > > User.reset_column_information > Role.reset_column_information > > > admin_user = User.create(:username => ''Admin'', >> :email => ''ali-86qk6qiIKr0@public.gmane.org'', >> :profile => ''Site Administrator'', >> :pwd => ''admin'', >> :pwd_confirmation => ''admin'') >> >> admin_role = Role.find_by_name(''Administrator'') >> admin_user.roles << admin_role >> end >> end >> > > end > > >> so after that I run db:migrate and I expect that a new user called >> Admin should be created int eh database and the roles_users database >> should have one record in it connecting user Admin and role >> Administrator... but my databases are empty. >> >> Any suggestion? I''m using rails 2.3.5 and I think the book uses an >> older version so I suspect something to do with a version difference, >> but not sure. >> >> Thanks in advance. >> > > > Before you use a Model inside a migration, it''s a good idea to create a > special class for it *inside* the migration (which is why I added the class > around your code). These models should have just enough content for the > purpose of the migration. I''m not sure about the HABTM relationships, but I > threw .reset_column_information calls in there for both models since the > roles_users table is new and common to both. > > One question though, why didn''t you create the Admin user when the User > table was created? Do you have logic to keep the last user with an > ''Administrator'' role from being deleted? > > -Rob > > Rob Biedenharn > http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > http://gaslightsoftware.com > rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org > > > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 18, 2010, at 11:19 PM, Ali Akhtarzada wrote:> Hi Rob, > > That would make more sense creating the Administrator at the time of > the users migration, but I''m following a book and the roles were > created later so I suppose the author just figured he''d throw it in > there or something. I do not have logic that prevents the admin from > being deleted at this point either. Noted :) > > Thanks. > > Is it standard practice to have the classes (Role and User in this > case) defined inside the migration RolesUsersJoin, wouldn''t it be > inconvenient since I would have my Role and User model defined > somewhere else anyway? Or are you saying I should not define a User > and Role elsewhere but just have them inside the RolesUsersJoin class? > > - AliYou want the normal models defined in their own files as is the convention. However, if you plan to manipulate the data through a model during a migration, then having a "local" version of that model defined as a nested class will shield you from problems later if the normal model evolves to have columns not present at the time of the migration that does the manipulation. (Adding a column to the table and then including a validation in the model is one way to do it and if you''re making small migrations, it''s not too hard to have a set of migrations that are deployed to production at the same time that are "incompatible" with each other.) Some will claim that this is being paranoid, but I''ve helped at least two other projects (not my own) when they encountered just such crises. If you are only doing "migration" things (i.e., Data Definition Language stuff in SQL-speak), then there''s no need for a nested class. -Rob> > On Wed, May 19, 2010 at 12:35 AM, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org > > wrote: > On May 18, 2010, at 1:32 AM, Ali wrote: > > Hi, so I''m new to rails development, and I''m following a book called > Practical Rails: Social Networking Sites. There''s a user role system > that the book develops, each user has a one or more roles and vice > versa. So I have the tables users, roles and roles_users (which > connects users to roles and vice versa). > > In the Role model I have has_and_belongs_to_many :users and in the > User model I have has_and_belongs_to_many: roles. > > To get the joint table I ran the command: script/generate migration > CreateRolesUsersJoin and in the 2010301_create_roles_users_join.rb > file I have the following: > > class CreateRolesUsersJoin < ActiveRecord::Migration > class User < ActiveRecord::Base > > has_and_belongs_to_many: roles > # include code as needed (like a before_save :encrypt_password) > end > class Role < ActiveRecord::Base > > has_and_belongs_to_many :users > end > > > def self.up > create_table :roles_users, :id => false do |t| > t.column :role_id, :integer, :null => false > t.column :user_id, :integer, :null => false > end > > User.reset_column_information > Role.reset_column_information > > > admin_user = User.create(:username => ''Admin'', > :email => ''ali-86qk6qiIKr0@public.gmane.org'', > :profile => ''Site Administrator'', > :pwd => ''admin'', > :pwd_confirmation => ''admin'') > > admin_role = Role.find_by_name(''Administrator'') > admin_user.roles << admin_role > end > end > > end > > > so after that I run db:migrate and I expect that a new user called > Admin should be created int eh database and the roles_users database > should have one record in it connecting user Admin and role > Administrator... but my databases are empty. > > Any suggestion? I''m using rails 2.3.5 and I think the book uses an > older version so I suspect something to do with a version difference, > but not sure. > > Thanks in advance. > > > Before you use a Model inside a migration, it''s a good idea to > create a special class for it *inside* the migration (which is why I > added the class around your code). These models should have just > enough content for the purpose of the migration. I''m not sure about > the HABTM relationships, but I threw .reset_column_information calls > in there for both models since the roles_users table is new and > common to both. > > One question though, why didn''t you create the Admin user when the > User table was created? Do you have logic to keep the last user with > an ''Administrator'' role from being deleted? > > -Rob > > Rob Biedenharn > http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > http://gaslightsoftware.com > rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org > > > > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . > > > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > .Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org +1 513-295-4739 Skype: rob.biedenharn -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.