Rails 问一个 Rails 5.0 版本问题

lengcb · 2017年09月19日 · 最后由 jasl 回复于 2017年09月19日 · 1176 次阅读

最近项目里遇到一个问题,用的是 rails5.0 版本。有一个多对多的关系

has_many :assign_objectives,->{uniq}, through: :assign_key_results,  source: :objective

这应该是 rails3,rails4 版本的写法,我在网上查了一下,只查到 rails5.1 的写法

has_many : assign_objectives, through: :assign_key_results, :uniq => true, source: :objective

请问 rails5.0 的正确写法是什么?

发现好像不是版本的问题,我在 console 里老是报这个错误

ArgumentError: Unknown key: :uniq. Valid keys are: :class_name, :anonymous_class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table, :foreign_type, :index_errors

错误提示告诉你 没有 uniq 这个参数

你希望应用层有唯一性验证的话,加唯一性验证器

lengcb 关闭了讨论。 09月20日 10:01
需要 登录 后方可回复, 如果你还没有账号请 注册新账号