Has_many :through basic fubar

I’m trying to do, what seems like, a very basic join. However, the
association just doesn’t seem to work. I’m sure it’s something very
simple but I don’t know what.

class Role < ActiveRecord::Base
belongs_to :packages
belongs_to :users
end

class Package < ActiveRecord::Base
has_many :roles
has_many :users, :through => :roles
end

class User < ActiveRecord::Base
has_many :roles
has_many :packages, :through => :roles
end

create_table “packages”, :force => true do |t|
t.string “tracking”
t.datetime “created_at”
t.datetime “updated_at”
end

create_table “roles”, :force => true do |t|
t.integer “user_id”
t.integer “package_id”
t.string “role”
end

create_table “users”, :force => true do |t|
t.string “user_name”
t.string “first”
t.string “last”
end

With that, I can do:

@package = Package.new
@roles = @package.roles

@roles.inspect # OK

But I can’t do:

@package = Package.new
@users = @package.users

or not:

@package.roles.collect { |a| a.users }
@users = @package.users

Nada…

Try to change :


class Role < ActiveRecord::Base
belongs_to :packages
belongs_to :users
end

with :

class Role < ActiveRecord::Base
belongs_to :package
belongs_to :user
end

it’s the one of the rails conventions, ‘belongs_to’ should take as
parameter
the singular name of the model :slight_smile:

I hope it’will if not post me the log errors.

2011/9/4 Dave G. [email protected]

Oh for Pete’s sake. I thought I tried that both ways… I probably had
some other error at the time. THANK YOU SO MUCH!!