Rails Model has_many with multiple foreign_keys

前端 未结 8 1079
说谎
说谎 2020-11-28 06:04

Relatively new to rails and trying to model a very simple family \"tree\" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is b

8条回答
  •  囚心锁ツ
    2020-11-28 06:14

    Used named_scopes over the Person model do this:

    class Person < ActiveRecord::Base
    
        def children
          Person.with_parent(id)
        end
    
        named_scope :with_parent, lambda{ |pid| 
    
           { :conditions=>["father_id = ? or mother_id=?", pid, pid]}
        }
     end
    

提交回复
热议问题