Rails has_many : Join Model의 추가 속성으로 찾기
Ruby와 Rails는 처음이지만 지금까지 책을 배웠습니다 (아무 의미도 없습니다. 하하).
EventUser 테이블을 통해 결합 된 두 가지 모델, Event 및 User가 있습니다.
class User < ActiveRecord::Base
has_many :event_users
has_many :events, :through => :event_users
end
class EventUser < ActiveRecord::Base
belongs_to :event
belongs_to :user
#For clarity's sake, EventUser also has a boolean column "active", among others
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :event_users
end
이 프로젝트는 일정표로, 특정 이벤트에 등록하고 이름을 긁는 사람들을 추적해야합니다. 다대 다가 좋은 접근 방식이라고 생각하지만 다음과 같이 할 수는 없습니다.
u = User.find :first
active_events = u.events.find_by_active(true)
이벤트에는 실제로 추가 데이터가 없기 때문에 EventUser 모델이 있습니다. 그리고 내가 할 수있는 동안 :
u = User.find :first
active_events = []
u.event_users.find_by_active(true).do |eu|
active_events << eu.event
end
이것은 "레일즈 방식"과 반대되는 것 같습니다. 누구든지 나를 깨달을 수 있습니까? 오늘 밤 (오늘 아침) 오랫동안 나를 괴롭 혔습니까?
이와 같은 것을 사용자 모델에 추가하는 것은 어떻습니까?
has_many :active_events, :through => :event_users,
:class_name => "Event",
:source => :event,
:conditions => ['event_users.active = ?',true]
그 후 다음을 호출하여 사용자에 대한 활성 이벤트를 가져올 수 있습니다.
User.first.active_events
Milan Novota는 좋은 솔루션을 가지고 있습니다. 그러나 :conditions
이제는 더 이상 사용되지 않으며 :conditions => ['event_users.active = ?',true]
어쨌든 그 비트는 그다지 레일처럼 보이지 않습니다. 나는 다음과 같은 것을 선호합니다.
has_many :event_users
has_many :active_event_users, -> { where active: true }, class_name: 'EventUser'
has_many :active_events, :through => :active_event_users, class_name: 'Event', :source => :event
그 후에도 다음을 호출하여 사용자에 대한 활성 이벤트를 가져올 수 있습니다.
User.first.active_events
u.events가 user_events 테이블을 명시 적으로 호출 하지 않더라도 필요한 조인으로 인해 해당 테이블은 여전히 SQL에 암시 적으로 포함됩니다 . 따라서 찾기 조건에서 해당 테이블을 계속 사용할 수 있습니다.
u.events.find(:all, :conditions => ["user_events.active = ?", true])
당신이 있는지 확인 후 많은이 조회를 수행 할 계획이라면 물론, 밀란 Novota에서 알 수 있듯이 그것을 별도의 연결을 제공,하지만이 없다 요구 사항 당신이 그런 식으로 작업을 수행 할 수는
글쎄요, User
실제로 필요한 것보다 더 많은 책임이 모델에 들어가고 있고 그렇게 할 타당한 이유가 없습니다.
먼저 EventUser
모델 에서 범위를 정의 할 수 있습니다. 실제로 속하는 위치는 다음과 같습니다.
class EventUser < ActiveRecord::Base
belongs_to :event
belongs_to :user
scope :active, -> { where(active: true) }
scope :inactive, -> { where(active: false) }
end
이제 사용자는 활성 이벤트와 비활성 이벤트를 모두 가질 수 있으므로 User
다음과 같이 모델 에서 관계를 정의 할 수 있습니다 .
class User < ActiveRecord::Base
has_many :active_event_users, -> { active }, class_name: "EventUser"
has_many :inactive_event_users, -> { inactive }, class_name: "EventUser"
has_many :inactive_events, through: :inactive_event_user,
class_name: "Event",
source: :event
has_many :active_events, through: :active_event_users,
class_name: "Event",
source: :event
end
이 기술의 EventUser
장점은 활성 또는 비활성 이벤트 의 기능 이 모델에 속한다 는 것입니다. 향후 기능을 수정해야하는 경우 한 곳 ( EventUser
모델) 에서만 수정되며 변경 사항이 모델에 반영됩니다. 다른 모든 모델.
'Development Tip' 카테고리의 다른 글
Apache Spark에서 Dataframe의 열 값을 List로 추출 (0) | 2020.10.31 |
---|---|
Android Studio 3.4로 업그레이드 한 후 resources_ap 관련 오류 (0) | 2020.10.31 |
참조 된 모든 어셈블리를 앱 도메인에 강제로로드하는 방법이 있습니까? (0) | 2020.10.31 |
MVVM을 사용하는 이유는 무엇입니까? (0) | 2020.10.31 |
"로그 앤 스로우"가 안티 패턴으로 간주되는 이유는 무엇입니까? (0) | 2020.10.31 |