简体   繁体   中英

has_one :through => multiple

Both Attendment & Vouching:

belongs_to :event
belongs_to :account

Therefore: 1 to 1 relationship between attendments and vouchings.

Is there a way to do this without my thinking too much?

# attendment
has_one :vouching :through => [:event, :account]

Note: I don't mind thinking too much, actually.

Yeah i don't think you can use a has_one for this. Assuming I'm reading this correctly, you have two models:

Attendment Vouching

They both store an event_id and account_id. You want to know from the attendment model, what vouching shares the same event and account as the attendment. I think the easiest solution for this is to write a method inside your attendment.rb file.

class Attendment < ActiveRecord::Base
  # belong to statements go here
  def voucher
    Voucher.where(:event_id => self.event_id, :account_id => self.account_id).first
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM