[英]How to get Collection object throug Many to Many relationship in Ruby on Rails
我关注的朋友
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:来自c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record / base.rb:1205:来自c的未初始化常量Project :: MilestonesProject 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:in 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'from 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
'/ RailsInstaller / Ruby1.8.7 / lib / ruby / gems / 1.8 / gems / activerecord-3.0.9 / lib / active_record / 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:in 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
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
initialize'from 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'from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib / active_record / associations.rb:1483:in new' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations.rb:1483:in
里程碑中'from(irb):2 from:0
朋友们请帮我
我需要执行一些命令,例如
milestone_count=project.milestones.size
谢谢你快速的回复! 终于我找到了解决方案,这里发布的答案都是正确的。
我这样改变了我的模特
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
我怀疑rails的命名约定就是问题所在。 Rails认为您的模型对象是单数,而数据库表是复数。 Rails可能会对名为MilestonesProjects
的模型产生问题,应将其命名为MilestoneProject
因为就是这样:一个Milestone
与一个Project
之间的关系。 看一下“ Active Record指南”第2.4节中的示例,其中使用单数和复数形式。
在您的MilestonesProjects模型中尝试此代码
class MilestonesProjects < ActiveRecord::Base
belongs_to :milestone,:foreign_key => 'milestone_id'
belongs_to :project,:foreign_key => 'project_id'
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.