There are two models: Companies and Employees table companies integer :ceo_id integer :cfo_id end table employees string :name end How should I implement <company instance>.ceo and <company instance>.cfo which will return the CEO''s and CFO''s employee records? Should I do: ------ (1) class Company belongs_to :ceo, :class_name => ''Employee'' belongs_to :cfo, :class_name => ''Employee'' end or (2) class Company has_one :ceo, :class_name => ''Employee'', :primary_key => :ceo_id, :foreign_key => :id has_one :cfo, :class_name => ''Employee'', :primary_key => :cfo_id, :foreign_key => :id end ----- (1) is definitely simpler, but I don''t quite get over the psychological hurdle why Company should ''belongs_to'' a ceo employee, but not ''has_one'' ceo. Am I missing something? -- View this message in context: http://old.nabble.com/Use-has_one-or-belongs_to-tp30209057p30209057.html Sent from the RubyOnRails Users mailing list archive at Nabble.com. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Nov 13, 2010 at 1:50 PM, merckmyers <merckmyers-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > There are two models: Companies and Employees > table companies > integer :ceo_id > integer :cfo_id > end > table employees > string :name > end > How should I implement <company instance>.ceo and <company instance>.cfo > which will return the CEO''s and CFO''s employee records? > Should I do: > ------ > (1) > class Company > belongs_to :ceo, :class_name => ''Employee'' > belongs_to :cfo, :class_name => ''Employee'' > end > or > (2) > class Company > has_one :ceo, :class_name => ''Employee'', :primary_key => :ceo_id, > :foreign_key => :id > has_one :cfo, :class_name => ''Employee'', :primary_key => :cfo_id, > :foreign_key => :id > end > ----- > (1) is definitely simpler, but I don''t quite get over the psychological > hurdle why Company should ''belongs_to'' a ceo employee, but not ''has_one'' > ceo. Am I missing something? >You might have a good reason, but what about just add a :title attribute to Employees. Then you just ask for Company.include("employees").where("employee.title=''ceo''")? (my query might not be exact but you get the idea....) Worst case you can add logic to your models to validate to only allow one ceo / cfo per company. Just a thought.> -- > View this message in context: > http://old.nabble.com/Use-has_one-or-belongs_to-tp30209057p30209057.html > Sent from the RubyOnRails Users mailing list archive at Nabble.com. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
yeah...David is right. Just add a boolean field for each of the attributes i.e ceo and cfo in the employees because if you look at the picture in real world, an employee could be either an ceo, an cfo or just an employee. On Sat, Nov 13, 2010 at 11:50 AM, merckmyers <merckmyers-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > There are two models: Companies and Employees > table companies > integer :ceo_id > integer :cfo_id > end > table employees > string :name > end > How should I implement <company instance>.ceo and <company instance>.cfo > which will return the CEO''s and CFO''s employee records? > Should I do: > ------ > (1) > class Company > belongs_to :ceo, :class_name => ''Employee'' > belongs_to :cfo, :class_name => ''Employee'' > end > or > (2) > class Company > has_one :ceo, :class_name => ''Employee'', :primary_key => :ceo_id, > :foreign_key => :id > has_one :cfo, :class_name => ''Employee'', :primary_key => :cfo_id, > :foreign_key => :id > end > ----- > (1) is definitely simpler, but I don''t quite get over the psychological > hurdle why Company should ''belongs_to'' a ceo employee, but not ''has_one'' > ceo. Am I missing something? > -- > View this message in context: > http://old.nabble.com/Use-has_one-or-belongs_to-tp30209057p30209057.html > Sent from the RubyOnRails Users mailing list archive at Nabble.com. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 13 November 2010 19:50, merckmyers <merckmyers-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > There are two models: Companies and Employees > table companies > integer :ceo_id > integer :cfo_id > end > table employees > string :name > end > How should I implement <company instance>.ceo and <company instance>.cfo > which will return the CEO''s and CFO''s employee records? > Should I do: > ------ > (1) > class Company > belongs_to :ceo, :class_name => ''Employee'' > belongs_to :cfo, :class_name => ''Employee'' > end > or > (2) > class Company > has_one :ceo, :class_name => ''Employee'', :primary_key => :ceo_id, > :foreign_key => :id > has_one :cfo, :class_name => ''Employee'', :primary_key => :cfo_id, > :foreign_key => :id > endThe second one will not work with the db schema you have shown, the id fields would have to be in the Employee. I would suggest maybe company has_many employees and employee belongs_to company and then use roles to specify their function. Google for Rails Roles and you will find many examples on how to do this. The simplest is just a role field in the employee record, but if you want an employee to be able to fill multiple roles then you probably need a Roles table.> ----- > (1) is definitely simpler, but I don''t quite get over the psychological > hurdle why Company should ''belongs_to'' a ceo employee, but not ''has_one'' > ceo. Am I missing something?No, you are not missing anything, sometimes the words just do not seem to match the real world. Colin -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn-3 wrote:> > On Sat, Nov 13, 2010 at 1:50 PM, merckmyers <merckmyers-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > (2) will work because of the re-definition of primary key and foreign key > in the has_one relationship > > There are definitely many ways to implement the <instance>.ceo method, > basically the query needs to take place is: > > select employee.* from employees where employees.id = <what ever the > ceo_id is in companies> > > Both (1) and (2) will achieve this, but (1) doesn''t quite make sense, and > (2) seems just hacking > > What I am really missing is that Rails is supposed to help us with all the > logic and makes the relationship more readable, but I just don''t see it in > such a case, and many other cases, too. Am I really missing something? > >> >> There are two models: Companies and Employees >> table companies >> integer :ceo_id >> integer :cfo_id >> end >> table employees >> string :name >> end >> How should I implement <company instance>.ceo and <company instance>.cfo >> which will return the CEO''s and CFO''s employee records? >> Should I do: >> ------ >> (1) >> class Company >> belongs_to :ceo, :class_name => ''Employee'' >> belongs_to :cfo, :class_name => ''Employee'' >> end >> or >> (2) >> class Company >> has_one :ceo, :class_name => ''Employee'', :primary_key => :ceo_id, >> :foreign_key => :id >> has_one :cfo, :class_name => ''Employee'', :primary_key => :cfo_id, >> :foreign_key => :id >> end >> ----- >> (1) is definitely simpler, but I don''t quite get over the psychological >> hurdle why Company should ''belongs_to'' a ceo employee, but not ''has_one'' >> ceo. Am I missing something? >> > > You might have a good reason, but what about just add a :title attribute > to > Employees. Then you just ask for > Company.include("employees").where("employee.title=''ceo''")? (my query > might > not be exact but you get the idea....) > > Worst case you can add logic to your models to validate to only allow one > ceo / cfo per company. > > Just a thought. > >> -- >> View this message in context: >> http://old.nabble.com/Use-has_one-or-belongs_to-tp30209057p30209057.html >> Sent from the RubyOnRails Users mailing list archive at Nabble.com. >> >> -- >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- View this message in context: http://old.nabble.com/Use-has_one-or-belongs_to-tp30209057p30209807.html Sent from the RubyOnRails Users mailing list archive at Nabble.com. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.