Note that there are some explanatory texts on larger screens.

plurals
  1. POUser has many roles through assignments
    primarykey
    data
    text
    <p>I want to be able to assign multiple roles to every user and get them back with something like User.first.roles, but I'm lost, how would you do it using rails console? What am I doing wrong in my models? </p> <pre><code>u = User.first User Load (0.1ms) SELECT "users".* FROM "users" LIMIT 1 =&gt; #&lt;User id: 18 [...]&gt; 1.9.3-p194 :004 &gt; u.roles NameError: uninitialized constant User::Assignment from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:119:in `compute_type' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in `klass' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/reflection.rb:385:in `block in source_reflection' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/reflection.rb:385:in `collect' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/reflection.rb:385:in `source_reflection' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/reflection.rb:508:in `check_validity!' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/associations/association.rb:26:in `initialize' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:24:in `initialize' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/associations/has_many_through_association.rb:10:in `initialize' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/associations.rb:157:in `new' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/associations.rb:157:in `association' from [path]/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/associations/builder/association.rb:44:in `block in define_readers' from (irb):4 from [path]/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start' from [path]/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start' from [path]/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands.rb:41:in `&lt;top (required)&gt;' from script/rails:6:in `require' from script/rails:6:in `&lt;main&gt;' </code></pre> <p>models/user.rb</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation, :role_id has_many :assignments has_many :roles, through: :assignments #more code... end </code></pre> <p>models/role.rb</p> <pre><code>class Role &lt; ActiveRecord::Base attr_accessible :name has_many :assignments has_many :users, through: :assignments end </code></pre> <p>models/assignments.rb</p> <pre><code>class Assignments &lt; ActiveRecord::Base attr_accessible :role_id, :user_id belongs_to :user belongs_to :role end </code></pre> <p>schema.rb: <a href="http://cl.ly/323n1t0Q1t390y1M2S0E" rel="nofollow">http://cl.ly/323n1t0Q1t390y1M2S0E</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload