Saturday, September 3, 2011

[Rails] 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...

--
Posted via http://www.ruby-forum.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-talk@googlegroups.com.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate