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?
- Ali
You 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 <r...@agileconsultingllc.com
> 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 => 'a...@ex.com',
: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
r...@agileconsultingllc.com
http://gaslightsoftware.com
r...@gaslightsoftware.com
--
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-
t...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com
.
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-
t...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com
.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
.
Rob Biedenharn http://agileconsultingllc.com
r...@agileconsultingllc.com
+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-t...@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.