2017-02-26 2 views
0

Ich habe eine Tabelle namens Rechnungen. Rechnungen has_many Sponsoren und has_many Cosponsors. Sowohl Sponsoren als auch Cosponsoren enthalten eine Liste von Kongressleuten. Ich habe auch eine Tabelle namens congress_people. Auf der congress_people show page möchte ich eine Tabelle anzeigen, die zeigt, welche Rechnungen die Kongress-Person sponsert und welche zeigt, welche Rechnungen sie sponsern. Irgendeine Idee, wie ich das machen könnte? Ich weiß, um die Tabellen zu generieren, aber ich bin nicht sicher, wie die Zuordnung eingerichtet wird. Ich verwende Schienen 4.2 und mysql2. DankEinrichten von Zuordnungen/db Referenzen Schienen 4

Antwort

0

Sie könnten eine has_many_through Verein verwenden, etwa wie folgt aus:

class Bill < ActiveRecord::Base 
    has_many :sponsorships, -> { where(kind: :primary) }, class_name: "Sponsorship" 
    has_many :cosponsorships, -> { where(kind: :secondary) }, class_name: "Sponsorship" 

    has_many :sponsors, class_name: 'CongressPerson', through: :sponsorships 
    has_many :cosponsors, class_name: 'CongressPerson', through: :cosponsorships 
end 

class Sponsorship < ActiveRecord::Base 
    # schema bill_id(INT), sponsor_id(INT), kind(STRING) 
    belongs_to :bill 
    belongs_to :sponsor, class_name: "CongressPerson" 
end 

class CongressPerson < ActiveRecord::Base 
    has_many :sponsorships, -> { where(kind: :primary) }, class_name: "Sponsorship", foreign_key: :sponsor_id 
    has_many :cosponsorships, -> { where(kind: :secondary) }, class_name: "Sponsorship", foreign_key: :sponsor_id 

    has_many :sponsored_bills, through: :sponsorships, source: :bill 
    has_many :cosponsored_bills, through: :cosponsorships, source: :bill 

end 

Hier ist eine grundlegende Migration für die Sponsorship Tabelle:

class CreateSponsorships < ActiveRecord::Migration 
    def change 
    create_table :sponsorships do |t| 
     t.references :bill 
     t.references :sponsor 
     t.string :kind 

     t.timestamps 
    end 
    end 
end 

Diese Sie können Anrufe wie folgt vorzunehmen:

@bill.sponsors 
# => returns the congress_people sponsoring the bill 

@bill.cosponsors 
# => returns the congress_people cosponsoring the bill 

@congress_person.sponsored_bills 
# => returns the bills sponsored by the congress person 

@congress_person.cosponsored_bills 
# => returns the bills cosponsored by the congress person 

Dann in Ihrem CongressPerson show view:

<%= @congress_person.sponsored_bills.each do |sponsored| %> 
    # populate your HTML table row 
<% end %> 
<%= @congress_person.cosponsored_bills.each do |cosponsored| %> 
    # populate your HTML table row 
<% end %> 
+0

Für die Sponsorentabelle würde ich add_reference für die bill_id & sponsor_id verwenden? – Taylor

+0

@ Taylor Ich habe gerade eine Beispielmigration für die "Sponsorship" -Tabelle hinzugefügt. Ich habe auch die aktiven Plattenbeziehungen optimiert, um das klarer zu machen. Ich hoffe es hilft! – user3680688

+0

Also habe ich etwas geändert und konnte mein Formular erscheinen lassen, aber anstatt ein Dropdown mit Namen darin zu bekommen, bekomme ich ein Textfeld mit # drin. Irgendwelche Ideen? – Taylor