【发布时间】:2016-09-03 04:45:06
【问题描述】:
我需要列出Inputs 有Translations 和language_id = 1 并且没有language_id = 2 的翻译
我目前的范围是:
scope :language, -> (id){joins(:translations).where('translations.language_id = 1)}
型号:
class Input < ActiveRecord::Base
has_many :translations
has_many :languages, through: :translations
scope :language, -> (id) {joins(:translations).where('translations.language_id = 1')}
def translation_from_language(language)
self.translations.where(language: language).take
end
end
class Translation < ActiveRecord::Base
belongs_to :input
belongs_to :language
before_save :default_values
#Apenas para testar o scope de search
scope :search, -> (part) { where("LOWER(value) LIKE ?", "%#{part.downcase}%") }
# Se nao setar um input manualmente, irá criar um novo
def default_values
self.input ||= Input.create
end
end
class Language < ActiveRecord::Base
has_many :translations
has_many :inputs, through: :translations
end
【问题讨论】:
标签: ruby-on-rails postgresql join scope