Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get Collection object throug Many to Many relationship in Ruby on Rails
    primarykey
    data
    text
    <p>Friends I've following models</p> <pre><code>class Project &lt; ActiveRecord::Base has_many :milestones_projects has_many :milestones, :through =&gt; :milestones_projects validates :projectname, :presence=&gt;true, :length=&gt; {:maximum=&gt;250}, :uniqueness=&gt;true validates :location,:presence=&gt;true end class Milestone &lt; ActiveRecord::Base has_many :milestones_projects has_many :projects, :through =&gt; :milestones_projects validates :name, :presence =&gt; true, :length =&gt; {:maximum =&gt; 250} validates :days, :presence =&gt; true, :length =&gt; {:maximum =&gt; 3} validates :stage, :presence =&gt; true end class MilestonesProjects &lt; ActiveRecord::Base belongs_to :milestones,:foreign_key =&gt; 'milestone_id' belongs_to :projects,:foreign_key =&gt; 'project_id' end </code></pre> <p>So i have many-to-many relationship between project and milestone.</p> <p>in irb, I want to get collection object, so I'm using as following</p> <pre><code>irb(main):001:0&gt; me=Project.find(1) =&gt; #&lt;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"&gt; </code></pre> <p>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</p> <pre><code>irb(main):002:0&gt; me.milestones </code></pre> <p>but it's giving me error like</p> <p>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 <code>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</code>send' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/reflection.rb:162:in <code>klass' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/reflection.rb:346:in</code>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 <code>collect' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/reflection.rb:346:in</code>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 <code>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</code>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 <code>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</code>initialize' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations.rb:1483:in <code>new' from c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/ active_record/associations.rb:1483:in</code>milestones' from (irb):2 from :0</p> <p>so friends please help me..</p> <p>i need to execute some commands like</p> <pre><code>milestone_count=project.milestones.size </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload