Hi All, I''m building an app that needs to make a comparison between two people and I''m wondering how to best build the models, and if I''m doing this correctly. I''m representing each partner as a User, so I wanted to keep just a user table and a comparisons table (the comparisons table will keep all the comparison data, the user model will hold all the data about that individual user) I''m thinking of making a User-to-User self-referential join through a "comparison" model. So, I''d have two models: the user model, and a comparison model The user model would look something like this: class User < ActiveRecord::Base belongs_to :partner, :through => :comparisons end and the comparisons: class Comparison < ActiveRecord::Base belongs_to :user has_one :partner, :class_name => "User", :foreign_key => "partner_id" end I honestly have no idea if this will work... does this look close? Thanks in advance! Dustin -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 10/28/07, Dustin Anderson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi All, > > I''m building an app that needs to make a comparison between two people > and I''m wondering how to best build the models, and if I''m doing this > correctly. > > I''m representing each partner as a User, so I wanted to keep just a user > table and a comparisons table (the comparisons table will keep all the > comparison data, the user model will hold all the data about that > individual user) > > I''m thinking of making a User-to-User self-referential join through a > "comparison" model. > > So, I''d have two models: the user model, and a comparison model > > The user model would look something like this: > class User < ActiveRecord::Base > belongs_to :partner, :through => :comparisons:through is not a valid option on belongs_to, you can only use it on :has_many .....> > I honestly have no idea if this will work... does this look close?Sorry, don''t think so. Maybe with a little more insight on what you''re trying to do with the comparison Model, someone can move you towards the goal. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dustin Anderson wrote:> I''m building an app that needs to make a comparison between two people > and I''m wondering how to best build the models, and if I''m doing this > correctly. > > I''m representing each partner as a User, so I wanted to keep just a user > table and a comparisons table (the comparisons table will keep all the > comparison data, the user model will hold all the data about that > individual user) > > I''m thinking of making a User-to-User self-referential join through a > "comparison" model. > > So, I''d have two models: the user model, and a comparison model > > The user model would look something like this: > class User < ActiveRecord::Base > belongs_to :partner, :through => :comparisons > end > > and the comparisons: > > class Comparison < ActiveRecord::Base > belongs_to :user > has_one :partner, :class_name => "User", :foreign_key => "partner_id" > end > > I honestly have no idea if this will work... does this look close?Did you get this to work yet? I just posted an updated example of a self-referential has_many :through on my blog: http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through You might find that helpful. If you really need an asymmetric join model, I''m interested in seeing what you put together. I was thinking about blogging an example of that too, but I couldn''t think up a good use for it. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dustin Anderson
2007-Oct-30 20:57 UTC
Re: self-referential :has_many :through relationship?
> Did you get this to work yet? I just posted an updated example of a > self-referential has_many :through on my blog: > http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through > You might find that helpful. > > If you really need an asymmetric join model, I''m interested in seeing > what you put together. I was thinking about blogging an example of that > too, but I couldn''t think up a good use for it. > > -- > Josh Susser > http://blog.hasmanythrough.comWell, what I''m trying to do is compare two people - through a photo... I''m using attachment_fu. Here''s what I have at this point... I actually did away with the User model all together and just made the comparisons hold all the foreign keys: Comparisons model: belongs_to :male_partner, :polymorphic => true belongs_to :female_partner, :polymorphic => true (Fields for comparisons): t.column "male_partner_id", :integer t.column "male_partner_type", :string t.column "female_partner_id", :integer t.column "female_partner_type", :string t.column "comparison_value", :float MalePartner has_many :comparisons, :as => :male_partner FemalePartner has_many :comparisons, :as => :female_partner That''s it, again, no idea if it will work... The reason I made the male_partner and female_partner polymorphic is to be able to use an image from the web instead of an uploaded image. This is for a dating site. Thanks for the blog Josh, I checked it out... very cool - a little more that I need for this (I think), but nonetheless, it''s cool to see that all those self-referential joins can be done. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---