Tag: 多态

validation多态父级的存在

我正在使用以下模型开发Rails 3.2应用程序: class User < ActiveRecord::Base # Associations belongs_to :authenticatable, polymorphic: true # Validations validates :authenticatable, presence: true # this is the critical line end class Physician < ActiveRecord::Base attr_accessible :user_attributes # Associations has_one :user, as: :authenticatable accepts_nested_attributes_for :user end 我要做的是validation用户是否始终拥有可validation的父级。 这本身工作正常,但在我的forms中,用户模型抱怨不存在authenticatable。 我正在使用以下控制器为新医生显示一个表单,该表单接受用户的嵌套属性: def new @physician = Physician.new @physician.build_user respond_to do |format| format.html # new.html.erb […]

将Rails多态性用于嵌套注释

我需要在Rails 3应用程序中构建一个嵌套的注释系统,该应用程序允许对许多模型(文章,post等)进行评论,并且正在讨论按照本文的内容编写我自己的解决方案。 有一些gem可供使用,例如使用awesome_nested_set的 acts_as_commentable_with_threading ,但是他们对我的需求感到臃肿。 我需要能够为多个模型添加注释 我需要能够无限深入地为评论添加评论 我需要能够有效地检索post,文章等的所有后代 我需要能够在适当的嵌套中有效地呈现注释 我的问题是,如果我能够解决我可能遇到的潜在问题。 我想避免走一条路只是为了走向死胡同。 我最初的担忧涉及有效地查询儿童。 比如说,获取一篇文章后代评论列表(儿童和儿童的孩子)。 有人对此有所了解吗? 谢谢。