朋友在Ruby中的許多關係,我下面的模型如何獲得Collection對象通過量很多,以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:未初始化的常量項目:: MilestonesProject from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/base.rb:1205:in 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
send' from 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
source_reflection' from 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
中初始化' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations/association_collection.rb:23:在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
中初始化' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations.rb :1483:從(IRB)在new' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations.rb:1483:in
里程碑 :2 來自:0
所以朋友們請幫我..
我需要執行像
milestone_count=project.milestones.size
你可以添加你的Rails項目(含里程碑,項目的一部分的模式,和milestone_projects)。你可以編輯你的文章,以便堆棧跟蹤縮進4個空格嗎? – mliebelt