简体   繁体   中英

Ruby on Rails Relationship between 3 models

I am following a tutorial made for Rails 3, I am using Rails 3.2 but think I am following it correctly but am getting an error, My classes are as follows and the error occurs when I use, task.admin_users_tasks

class Task < ActiveRecord::Base

  attr_accessible :name, :project_id, :task_id, :permalink, :visible, :position, :created_at
  belongs_to :project
  has_and_belongs_to_many :admin_users
  has_many :admin_users_tasks
end

class AdminUsersTask < ActiveRecord::Base
  attr_accessible :admin_user_id, :task_id, :created_at, :updated_at
  belongs_to :admin_user, :class_name => "AdminUser" :foreign_key => 'admin_user_id'
  belongs_to :task
end

class AdminUser < ActiveRecord::Base

  attr_accessible :first_name, :last_name, :username, :email

  has_and_belongs_to_many :projects
  has_and_belongs_to_many :tasks
  has_many :admin_users_tasks
  has_many :admin_users_projects
end

Rails Console

    1.9.3-p362 :032 > task
 => #<Task id: 1, project_id: 6, permalink: "taskytask", position: 1, visible: true,   created_at: "2013-02-21 05:08:01", updated_at: "2013-02-21 05:08:01", name: "Task Name for   Noname Project"> 
1.9.3-p362 :033 > me
 => #<AdminUser id: 1, first_name: "Larry", last_name: "David", email:         "larry.david@mail.com", hashed_password: "c9f4e8d3aaa265033c2f517abd5d347bd81d67fa",     created_at: "2013-02-21 04:43:20", updated_at: "2013-02-21 04:43:20", username: "larrydavid",     salt: "d4189f8db685776d3a3c3c4d0700786b7879362d"> 
1.9.3-p362 :034 > me.admin_users_tasks
 => [] 
1.9.3-p362 :035 > task.admin_users_tasks
NoMethodError: undefined method `admin_users_tasks' for #<Task:0x007fca9e159f58>
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/activemodel-    3.2.11/lib/active_model/attribute_methods.rb:407:in `method_missing'
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-    3.2.11/lib/active_record/attribute_methods.rb:149:in `method_missing'
    from (irb):35
        from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/railties-    3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/railties-    3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/railties-    3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3-p362 :036 > 

That code is a bit awkward, mostly because your using HABTM syntax along with providing a model, which your supposed to do with has_many :through. I would setup the models differently, and then rails might be a little less confused.

class Task < ActiveRecord::Base
  has_many :admin_users_tasks
  has_many :admin_users, :through => :admin_users_tasks
end

class AdminUser < ActiveRecord::Base
  has_many :admin_users_tasks
  has_many :tasks, :through => :admin_users_tasks
end

class AdminUserTasks < ActiveRecord::Base
  belongs_to :admin_user
  belongs_to :task
end

HABTM is for when you want a Many-Many join, and there is where you have no model for the join table, just 2 columns for each side of the join. No ID or timestamps.

has_many :through is for when you want a Many-Many join, and there is a model for the join table, making it actually a double Many-One join, and the join table has a id column, timestamps, along with the two foreign keys.

Also by the looks of your code your doing the same thing with AdminUser and Project, if there is an AdminUserProject model joining those two models, use has_many :through not HABTM.

Fixed the problem. Firstly the console had not been restarted.

Secondly in rails 3.2 what was,

belongs_to :admin_user, :class_name => "AdminUser" :foreign_key => 'admin_user_id'

Should be

belongs_to :admin_user, class_name: "AdminUser", foreign_key: 'admin_user_id'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM