ruby on rails - Multiple associations in same table with multiple potential foreign keys -


i want seemingly simple associations:

class orderwizard < activerecord::base   belongs_to :buyer_wizard,     :class_name => miniwizard.name   belongs_to :seller_wizard,    :class_name => miniwizard.name end  class miniwizard < activerecord::base    has_one :order_wizard, :foreign_key = '????' # buyer_wizard_id or seller_wizard_id     def is_buyer_wizard?      ??    end     def is_seller_wizard?      ??    end end 
  1. an associated miniwizard needs know connecting it. assuming has_many :through best way go? if so, how models look?

  2. a miniwizard instance needs know if it's buyer or seller. stuck on how this.

you want 2 associations, example, might represent relationships more accurately:

class miniwizard < activerecord::base    has_one :bought_order_wizard, :foreign_key => 'buyer_wizard_id', :class_name => 'miniwizard'    has_one :sold_order_wizard, :foreign_key => 'seller_wizard_id', :class_name => 'miniwizard'    def order_wizard     bought_order_wizard || sold_order_wizard   end    def is_buyer_wizard?     !bought_order_wizard.nil?   end    def is_seller_wizard?     !sold_order_wizard.nil?   end end 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -