Rails ActiveRecord: 2 associations to same model -
i have model, project, has 2 belongs_to associations same model:
belongs_to :source_connection, class: connection belongs_to :destination_connection, class: connection
but on other side, each connection should have 1 association project. adding
has_one :project, foreign_key_id: source_connection_id or has_one :project, foreign_key_id: destination_connection_id
isn't going cutting it, because connection has no knowledge of whether it's source or destination connection.
clearly there's flaw in how i've designed association, i'm curious right way go is. it's worth mentioning 'connection' has lot of inheriting classes (sshconnection, s3connection, etc), splitting connection class 'source' , 'destination' connection models cause lot of duplication.
any thoughts welcome.
you're close! redesign, or seek naming projects differently different connection types:
has_one :source_project, class: "project", foreign_key_id: source_connection_id or has_one :destination_project, class: "project", foreign_key_id: destination_connection_id
this allows phone call @connection.source_project project connection source connection, example. there may clearer way name goals.
ruby-on-rails activerecord
No comments:
Post a Comment