Friends I've following models
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
So i have many-to-many relationship between project and milestone.
in irb, I want to get collection object, so I'm using as following
开发者_运维百科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">
here 'me' is an array contains first project details, and now i"m trying to get milestone details associated with this project. for this I'm trying as follows
irb(main):002:0> me.milestones
but it's giving me error like
NameError: uninitialized constant Project::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:in 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: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
milestones'
from (irb):2
from :0
so friends please help me..
i need to execute some commands like
milestone_count=project.milestones.size
thanks for your quick response! Finally i found the solution, both the answers posted here are correct..
i changed my models like this
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
I suspect that the naming conventions of rails are here the problem. Rails thinks that your model objects are singular, and your data base tables are plural. Rails could have a problem with a model named MilestonesProjects
, it should be named MilestoneProject
because that is it: the relation between one Milestone
and one Project
. Have a look at the example in "Guide to Active Records", section 2.4 and where singular and plural is used.
try this code in your MilestonesProjects model
class MilestonesProjects < ActiveRecord::Base
belongs_to :milestone,:foreign_key => 'milestone_id'
belongs_to :project,:foreign_key => 'project_id'
end
精彩评论