2015-08-15 70 views
0

我有以下的模型建立UNIQ /显着成果:通过协会通过配置Rails的

class Tale < ActiveRecord::Base 
    has_many :tale_culture_joins 
    has_many :cultures, through: :tale_culture_joins, dependent: :destroy, crud: true, index: true 
    has_many :tale_purpose_joins 
    has_many :purposes, through: :tale_purpose_joins, dependent: :destroy, crud: true, index: true 
    has_many :tale_book_joins 
    has_many :books, through: :tale_book_joins, dependent: :destroy, crud: true, index: true 
    has_many :tale_keyword_joins 
    has_many :keywords, through: :tale_keyword_joins, dependent: :destroy, crud: true, index: true 
    has_many :tale_character_joins 
    has_many :characters, through: :tale_character_joins, dependent: :destroy, crud: true, index: true 
    has_many :tale_moral_joins 
    has_many :morals, through: :tale_moral_joins, dependent: :destroy, crud: true 
    has_many :values, through: :morals, index: true 
    has_many :tale_event_joins 
    has_many :events, through: :tale_event_joins, dependent: :destroy, crud: true 

    end 

    Tale -> Moral -> Value 
    Tale(1) -> Moral(1,2) 
    Moral(1) -> Value(1,2) 
    Moral(2) -> Value(2,3) 
    Tale(1).values # => [1,2,2,3] 

我明白它背后的推理,我可以配置的轨道,不仅恢复,而且查询/累积回报值与uniqs,以便uniq条件传递到SQL查询本身

回答

0

如果我理解正确,您希望能够,例如,做Table.events并获得唯一事件的集合。你可以得到这个行为events这样的:

has_many :events, -> {uniq}, through: :tale_event_joins, dependent: :destroy, crud: true 

您可以包括在您想要一个独特的收藏任何关系类似-> {uniq}范围。