简体   繁体   English

Ruby on Rails 3个模型之间的关系

[英]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 我正在遵循为Rails 3制作的教程,我正在使用Rails 3.2,但认为我正确地遵循了它,但是遇到错误,我的类如下,当我使用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 Rails控制台

    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. 该代码有点尴尬,主要是因为您使用了HABTM语法并提供了一个模型,您应该使用has_many:through来完成。 I would setup the models differently, and then rails might be a little less confused. 我会以不同的方式设置模型,然后使Rails的混乱程度有所减轻。

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. HABTM适用于您想要多对多联接的情况,并且那里没有联接表的模型,联接的每一侧只有2列。 No ID or timestamps. 没有ID或时间戳。

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. has_many:through适用于您想要进行多对多联接的情况,并且该联接表有一个模型,实际上使它成为双重多对一联接,并且联接表具有一个id列,时间戳以及两个外部键。

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. 同样,从代码的外观来看,您对AdminUser和Project进行的操作相同,如果存在将这两个模型连接在一起的AdminUserProject模型,请使用has_many:through而不是HABTM。

Fixed the problem. 解决了问题。 Firstly the console had not been restarted. 首先,控制台尚未重新启动。

Secondly in rails 3.2 what was, 其次在rails 3.2中,

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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