I have Class Referral which takes both a giver and receiver as a Person. As is, @person.referrals returns all referrals as appropriate. Using built in AR methods, @referral.person returns the receiver of the referral. To find out who issued the referral, I have created an instance method "giver" so that @referral.giver returns the Person who gave it. I looked at Chad Fowler''s "Self-Referential MtM Models" but I''m not sure my situation is the same. The current setup I have works, but I''m not sure if there is a better Rails way to do this.>MIGRATIONclass CreateReferrals < ActiveRecord::Migration def self.up create_table :referrals do |t| t.column :giver_id, :integer, :null => false t.column :receiver_id, :integer, :null => false t.column :body, :text, :null => false t.column :created_at, :datetime end>REFERRAL MODELclass Referral < ActiveRecord::Base belongs_to :person validates_uniqueness_of :giver_id, :scope => :receiver_id def giver Person.find(self.giver_id) end>PERSON MODELclass Person < ActiveRecord::Base has_many :referrals, :foreign_key => "receiver_id" -- 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 -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-19 20:19 UTC
Re: Class that belongs_to another Class twice?
On Dec 19, 2:59 pm, Taylor Strait <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have Class Referral which takes both a giver and receiver as a Person. > As is, @person.referrals returns all referrals as appropriate. Using > built in AR methods, @referral.person returns the receiver of the > referral. To find out who issued the referral, I have created an > instance method "giver" so that @referral.giver returns the Person who > gave it. I looked at Chad Fowler''s "Self-Referential MtM Models" but > I''m not sure my situation is the same. The current setup I have works, > but I''m not sure if there is a better Rails way to do this. > > >MIGRATIONclass CreateReferrals < ActiveRecord::Migration > def self.up > create_table :referrals do |t| > t.column :giver_id, :integer, :null => false > t.column :receiver_id, :integer, :null => false > t.column :body, :text, :null => false > t.column :created_at, :datetime > end > > >REFERRAL MODELclass Referral < ActiveRecord::Base > belongs_to :personhas_one :giver, :class_name => "Person"> > validates_uniqueness_of :giver_id, :scope => :receiver_id > > def giver > Person.find(self.giver_id) > end > > >PERSON MODELclass Person < ActiveRecord::Base > > has_many :referrals, :foreign_key => "receiver_id" > > -- > Posted viahttp://www.ruby-forum.com/.I think you should be able to just add the has_one association to the referral model and get your attribute accessor for free... Best, -r -- Ryan Raaum http://raaum.org http://rails.raaum.org -- Rails docs http://locomotive.raaum.org -- Self contained Rails for Mac OS X --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
>> REFERRAL MODELclass Referral < ActiveRecord::Base >> belongs_to :person >> has_one :giver, :class_name => "Person"Unfortunately this does not work:>VIEW CODE<% for referral in @referrals %> <tr> <td>#{referral.giver.full_name}</td> <td><%= referral.body %></td> </tr>>ERRORMysql::Error: #42S22Unknown column ''people.referral_id'' in ''where clause'': SELECT * FROM people WHERE (people.referral_id = 4) LIMIT 1 -- 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 -~----------~----~----~----~------~----~------~--~---
>> has_one :giver, :class_name => "Person", :foreign_key => "giver_id"I knew I should have posted that I tried that too. Well, I tried that too! The thing is that AR is doing:>> belongs_to :person >> has_one :giver, :class_name => "Person"SELECT * FROM people WHERE (people.referral_id = 4) LIMIT 1>> belongs_to :person >> has_one :giver, :class_name => "Person", :foreign_key => "giver_id"SELECT * FROM people WHERE (people.giver_id = 4) LIMIT 1 when it should be doing: SELECT * FROM people WHERE (people.id = 4) LIMIT 1 -- 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 -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-19 23:40 UTC
Re: Class that belongs_to another Class twice?
On Dec 19, 5:12 pm, Taylor Strait <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> >> has_one :giver, :class_name => "Person", :foreign_key => "giver_id"I knew I should have posted that I tried that too. Well, I tried that > too! The thing is that AR is doing: > > >> belongs_to :person > >> has_one :giver, :class_name => "Person"SELECT * FROM people WHERE (people.referral_id = 4) LIMIT 1 > > >> belongs_to :person > >> has_one :giver, :class_name => "Person", :foreign_key => "giver_id"SELECT * FROM people WHERE (people.giver_id = 4) LIMIT 1 > > when it should be doing: > > SELECT * FROM people WHERE (people.id = 4) LIMIT 1 > > -- > Posted viahttp://www.ruby-forum.com/.Ok. Here''s what I''ve got... ## Migrations create_table "people" do |t| t.column "name", :string end create_table "referrals" do |t| t.column "giver_id", :integer t.column "receiver_id", :integer end ## Models class Person < ActiveRecord::Base has_many :referrals, :foreign_key => "receiver_id" end class Referral < ActiveRecord::Base belongs_to :receiver, :class_name => "Person", :foreign_key => "receiver_id" belongs_to :giver, :class_name => "Person", :foreign_key => "giver_id" end ## Console r = Person.create g = Person.create ref = Referral.create(:giver => g, :receiver => r) r.reload g.reload r.referrals #=> [ ref ] g.referrals #=> [] ref.giver #=> g ref.receiver #=> r Best, r -- Ryan Raaum http://raaum.org http://rails.raaum.org -- Rails docs http://locomotive.raaum.org -- Self contained Rails for Mac OS X --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Works great, Ryan. Much less hackish than my original solution. Thanks everyone. -- 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 -~----------~----~----~----~------~----~------~--~---