In the code posted Department model has_many: departments Should it not be has_many:customers???
Regards, Naren On Mon, Dec 14, 2009 at 3:46 PM, Todd <[email protected]> wrote: > I'm new and I just trying to figure out something about associations. > The following ruby code works but does not work in my actual rails > application. Am I doing something wrong or is there a reason why I > need to always use an extra table for a has_many_and_belongs_to > relationship? When I try this is Rails its willing to do a part of > this > (e.g, e.department) but not the last '.customers' part? I could of > course just to x = e.department and then do x.customers (which does > work in Rails) but why do I need to do this in two steps instead of > one? I can see that for large datasets why the additional table would > help but if the dataset is small why do you need the extra table/ > steps? > > require 'rubygems';require 'active_record'; require 'mysql'; > ActiveRecord::Base.establish_connection(:adapter => > 'mysql', :database => "idea", :username => "myname", :password => > 'mypswd') > > class Employee < ActiveRecord::Base > belongs_to :department > end > class Department < ActiveRecord::Base > has_many :departments > has_many :comments > end > class Customers < ActiveRecord::Base > belongs_to :department > end > e = Employee.find(1) > #to find the number of customers associated with a particular employee > within a department > p e.department.customers.size > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<rubyonrails-talk%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

