2012-03-26 14 views
3

Ich habe ein Benutzermodell und ein Account-Modell. Der Benutzer hat viele Konten und die Konten gehören einem Benutzer. Ich habe die Modelle und Assoziationen alle eingerichtet. Jetzt möchte ich eines dieser Konten zum "primären Konto" machen. Was ist der beste Weg, um die Assoziationen einzurichten? Ich habe eine Spalte primary_account_id zu meiner Benutzertabelle hinzugefügt und die Assoziationen so eingerichtet, aber es hat nicht funktioniert. Irgendwelche Tipps?Mehrere Zuordnungen in einem Modell

class User < ActiveRecord::Base 
    has_many :accounts 
    has_one :primary_account, :class_name => "Account" 
end 

class Account < ActiveRecord::Base 
    belongs_to :user 
end 

bearbeiten

Ich sehe diese Frage Rails model that has both 'has_one' and 'has_many' but with some contraints, die sehr ähnlich ist, und die zweite Antwort macht den Vorschlag, dass ich versucht. Jedoch, wenn ich es verwenden Schienen die Spalte ignoriert, die ich gemacht habe und greift nur die erste in der Tabelle:

>> u = User.find(1) 
    User Load (3.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
=> #<User id: 1, email: "[email protected]", created_at: "2012-03-15 22:34:39", updated_at: "2012-03-15 22:34:39", primary_account_id: nil> 
>> u.primary_account 
    Account Load (0.1ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."user_id" = 1 LIMIT 1 
=> #<Account id: 5, name: "XXXXXX", created_at: "2012-03-16 04:08:33", updated_at: "2012-03-16 17:57:53", user_id: 1> 
>> 
+1

Möglicherweise: 'has_one: primary_account,: class_name =>" Konto ",: conditions =>" users.primary_account_id = accounts.id "'. – Zabba

+0

Gut geraten, aber nein: 'Konto Laden (0.2ms) SELECT" Konten ". * FROM" Konten "WHERE" Konten "." Benutzer_ID "= 1 AND (users.primary_account_id = accounts.id) LIMIT 1 SQLite3 :: SQLException: keine solche Spalte: users.primary_account_id: SELECT "accounts". * FROM "accounts" WHERE "accounts". "User_id" = 1 UND (users.primary_account_id = accounts.id) LIMIT 1' – jasonlfunk

Antwort

6

Also habe ich eine einfache ERD und Ihr Problem ist sehr einfach, aber ich denke, ich fand ein ernstes Problem:

simple erd

class User < ActiveRecord::Base 
    has_many :accounts 
    has_one :primary_account, :class_name => "Account", :primary_key => "account_pimary_id" 
end 

class Account < ActiveRecord::Base 
    belongs_to :user 
end 

Um die Verbände zu erhalten wie es ist, setzen sie einfach die :primary_key auf has_one :primary_account so dass es users.account_primary_id statt users.id verwendet.

Während dies funktioniert, wird es wahrscheinlich nichts als Probleme verursachen. Wenn das Konto user_id des Kontos als Fremdschlüssel für id und account_primary_id verwendet wird, haben Sie keine Ahnung, ob ein Konto ein normales Konto oder ein primäres Konto ist, ohne jedes Mal sowohl id als auch account_primary_id zu verbinden. A foreign_key sollte nur auf 1 Spalte zeigen, in diesem Fall die Tabelle id des Benutzers. Dann ist es eine direkte Aufnahme in den Account-Tisch.

@Zabba Lösung ist der Schlaue, sondern braucht nur die :include für das beitreten

has_one :primary_account, :class_name => "Account", :conditions => "users.primary_account_id = accounts.id", :include => :user 

Dies bedeutet, dass alle Konten zu einem Benutzer gehören, und nur 1 als primäres Konto Liste stehen. Nett und geradlinig, die verrückten Where-Klauseln vermeidend.

+0

Vielen Dank! --- --- – jasonlfunk

Verwandte Themen