#7 ✓invalid
Glen Barnes

Ability to set habtm associations

Reported by Glen Barnes | May 19th, 2008 @ 06:54 PM

Maybe I'm doing this wrong but I have:


admin_role = Role.find(:first, :conditions => "title = 'admin'")
user_role = Role.find(:first, :conditions => "title = 'user'")
presenter_role = Role.find(:first, :conditions => "title = 'presenter'")

User.seed(:email) do |s|
  s.email = "admin@somewhere.com" 
  s.first_name = "Admin" 
  s.last_name = "User" 
  s.password = "password"
  s.password_confirmation = "password"
  s.roles << admin_role
  s.roles << user_role
end

I get the error "undefined method `roles' for #"

I have tried running the following in the console and it works:


u = User.find(:first, :conditions => "email = 'admin@somewhere.com'")
admin_role = Role.find(:first, :conditions => "title = 'admin'")
u.roles << admin_role
u.save

Can this be supported?

Comments and changes to this ticket

  • Michael Bleigh

    Michael Bleigh May 20th, 2008 @ 06:43 AM

    • State changed from “new” to “invalid”

    The problem is that you are trying to tack on a habtm to a not-yet-created record. This is not and will not be possible as it has not yet been assigned an id. However, something like this should work:

    admin_role = Role.find(:first, :conditions => "title = 'admin'")
    user_role = Role.find(:first, :conditions => "title = 'user'")
    presenter_role = Role.find(:first, :conditions => "title = 'presenter'")
    
    admin = User.seed(:email) do |s|
      s.email = "o:admin@somewhere.com">admin@somewhere.com" 
      s.first_name = "Admin" 
      s.last_name = "User" 
      s.password = "password"
      s.password_confirmation = "password"
    end
    
    RoleAssignmentModel.seed(:user_id, :role_id) do |s|
      s.user_id = admin.id
      s.role_id = admin_role.id
    end
    
    RoleAssignmentModel.seed(:user_id, :role_id) do |s|
      s.user_id = admin.id
      s.role_id = user_role.id
    end
    
  • Glen Barnes

    Glen Barnes May 20th, 2008 @ 07:18 AM

    Thanks for the pointer. This actually worked:

    
    user = User.seed(:email) do |s|
      s.email = "admin@somewhere.com" 
      s.first_name = "Admin" 
      s.last_name = "User" 
      s.password = "password"
      s.password_confirmation = "password"
    end
    
    user.roles << admin_role
    user.roles << user_role
    
    
  • Timothy Jones

    Timothy Jones July 31st, 2009 @ 01:37 PM

    • Tag set to feature, request

    Maybe I'm missing something, but the solution involving RoleAssignmentModel doesn't work for most HABTM associations, because the join table is ID-less and there is no assignment model.

    The best I've come up with is something like:

    user.roles << admin_role unless user.roles.include?(admin_role)

    (the include? check is necessary when the data has been previously seeded.)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

An attempt to simplify the usage of seed data in Rails applications through a dead simple API.

People watching this ticket

Pages