has_many 및 through 관계를 이해하는 데 문제가 있습니다. 난 여기에 몇 가지 다른 게시물을보고 정말 명확하지 않았다. (확실하지 않기 때문에 내가 뭘 하려는지 ..) 빌드 및 응용 프로그램 사용자가 일정 및 각 사용자가 이벤트 서로간에 이벤트를 교환 할 수 있습니다.레일 has_many : through
"user.trades"<과 같은 API를 사용하여 각 사용자에 대한 데이터를 검색 할 수 있기를 원합니다. 사용자가 작성한 모든 거래와 다른 메소드를 검색합니다. 하나는 "user.requested_trades"이고 다른 "user.pending_trades". requested_trades와 pending_trades는 작동하지만 3면 테이블 관계를 작성해야합니다. 그렇지 않으면이 2 가지 방법으로 충분합니다. 사실 진실로 콘솔을 사용하여 두 가지 방법을 수행하기 위해 실행되는 쿼리의 유형을 파악해야했습니다. 그러나 그것의 아직도 아주 불분명하다 : through와 : has_many.
class Trade < ActiveRecord::Base
belongs_to :seller,
:class_name => "User",
:foreign_key => "seller_id"
belongs_to :buyer,
:class_name => "User",
:foreign_key => "buyer_id"
end
class User < ActiveRecord::Base
has_many :events, :dependent => :destroy
has_many :requested_trades, -> { where(["trades.status = ?",'requested']).order("trades.created_at DESC") },
:class_name => "Trade",
:foreign_key => "buyer_id"
has_many :pending_trades, -> { where(["trades.status = ?",'pending']).order("trades.created_at DESC") },
:class_name => "Trade",
:foreign_key => "buyer_id"
has_many :sent_messages, -> { where(["messages.sender_deleted = ?", false]).order("messages.created_at DESC")},
:class_name => "Message",
:primary_key => "email",
:foreign_key => "sender_id"
has_many :received_messages, -> { where(["messages.recepient_deleted = ?", false]).order("messages.created_at DESC")},
:class_name => "Message",
:primary_key => "email",
:foreign_key => "recepient_id"
이 거래는 또한 이벤트에 연결되지해야 레일 가이드의 링크를 살펴 걸릴? 아니면 그들이 거래하는 것을 어떻게 압니까? – nathanvda
잘 나는 user.event로 각 사용자 이벤트에 액세스 할 수 있으므로 사용자에게 거래를 연결하는 것만으로도 충분할 것이라고 생각합니다. – Woody008
하지만 사용자는 많은 이벤트를 가지고 있으므로 어떤 이벤트가 거래에 있는지 어떻게 알 수 있습니까 ???? 아니면 항상 모든 이벤트를 교환합니까? – nathanvda