如何通过Ruby on Rails中的多对多关系获取Collection对象

时间:2011-09-17 06:01:53

标签: ruby-on-rails many-to-many

我跟随模特的朋友

class Project < ActiveRecord::Base
  has_many :milestones_projects
  has_many :milestones, :through => :milestones_projects
  validates :projectname, :presence=>true, :length=> {:maximum=>250}, :uniqueness=>true
  validates :location,:presence=>true
end

class Milestone < ActiveRecord::Base
  has_many :milestones_projects
  has_many :projects, :through => :milestones_projects

  validates :name, :presence => true, :length => {:maximum => 250}
  validates :days, :presence => true, :length => {:maximum => 3}
  validates :stage, :presence => true
end


class MilestonesProjects < ActiveRecord::Base
  belongs_to :milestones,:foreign_key => 'milestone_id'
  belongs_to :projects,:foreign_key => 'project_id'
end

所以我在项目和里程碑之间有多对多的关系。

在irb中,我想获取集合对象,所以我使用如下

irb(main):001:0> me=Project.find(1)
=> #<Project id: 1, location_id: 1, projectname: "Sample 2", description: "Sample 2", i
spublished: true, created_at: "2011-09-02 08:26:41", updated_at: "2011-09-07 10:26:48">

这里'我'是一个包含第一个项目详细信息的数组,现在我正在尝试获得与此项目相关的里程碑详细信息。为此我尝试如下

irb(main):002:0> me.milestones

但它给我的错误就像

NameError:未初始化的常量Project :: MilestonesProject         来自c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record / base.rb:1205:在compute_type' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/reflection.rb:162:in发送'         来自c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record / reflection.rb:162:在klass' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/reflection.rb:346:in source_reflection'中         来自c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record / reflection.rb:346:在collect' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/reflection.rb:346:in source_reflection'中         来自c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record / reflection.rb:377:在check_validity!' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations/association_proxy.rb:61:in初始化'         来自c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record / associations / association_collection.rb:23:in initialize' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations/has_many_association.rb:11:in initialize'         来自c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record / associations.rb:1483:在new' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations.rb:1483:in里程碑'         来自(irb):2         来自:0

所以朋友请帮帮我..

我需要执行一些命令,比如

milestone_count=project.milestones.size

3 个答案:

答案 0 :(得分:1)

感谢您的快速回复! 最后我找到了解决方案,这里发布的答案都是正确的。

我改变了我的模型

class Project < ActiveRecord::Base
  has_many :project_milestones
  has_many :milestones, :through => :project_milestones
end

class Milestone < ActiveRecord::Base  
  has_many :project_milestones
  has_many :projects, :through => :project_milestones
end

class ProjectMilestone < ActiveRecord::Base
  belongs_to :milestone,:foreign_key => 'milestone_id'
  belongs_to :project,:foreign_key => 'project_id'
end

答案 1 :(得分:0)

我怀疑rails的命名约定是问题所在。 Rails认为您的模型对象是单数,而您的数据库表是复数。 Rails可能会遇到名为MilestonesProjects的模型的问题,它应该命名为MilestoneProject,因为它是:MilestoneProject之间的关系。请查看"Guide to Active Records"第2.4节中的示例以及使用单数和复数的示例。

答案 2 :(得分:0)

在MilestonesProjects模型中尝试此代码

class MilestonesProjects < ActiveRecord::Base
  belongs_to :milestone,:foreign_key => 'milestone_id'
  belongs_to :project,:foreign_key => 'project_id'
end