我正在开发一个具有几种不同模型(门票,帖子,报告等)的应用程序.每个模型的数据都不同,我想从所有那些显示全面最新10个条目的模型中创建一个"提要"(所有数据的混合).
最好的方法是什么?我是否应该在为用户分配故障单或发布新报告时创建新的Feed模型并写入该表?我们也一直在寻找STI来构建一个模型引用表,或者只是创建一个聚合数据的类方法.不确定哪种方法效率最高......
我有评论和文章,都是可以投票的.
所以,基本上我有三个实体Article,Comment和Vote.
在一些阅读后在Doctrine2单表继承参考手册,似乎它就是我所需要的,因为我Vote仍然是相同的过度Article或Comment.
在ORM视图中,以下是我查看Vote表格的方式:
id | resource_id | resource_type | weight |
我想resource_type应该是"鉴别器"列,但我真的不明白如何在我的实体中实现它.
我想要做的是避免必须为我的每个实体投票表,因为投票实体对于两者都保持相同,除了"resource_type",所以我试图在Doctrine2中找到一种方式能够只有一个Vote实体可以使用.
(See below for link to sample project)
WHAT I HAVE WORKING:
I have many user types which I am handling using Single Table Inheritance in Rails, e.g.:
class User < ActiveRecord::Base
self.inheritance_column = :meta_type
scope :doctors, -> { where(meta_type: 'Doctor') }
scope :patients, -> { where(meta_type: 'Patient') }
scope :nurses, -> { where(meta_type: 'Nurse') }
scope :employees, -> { where(meta_type: 'Employee') }
end
class Doctor < User
has_many :doctor_patient_relations
has_many :patients, :through => :doctor_patient_relations
has_many :doctor_nurse_relations
has_many :nurses, :through => …Run Code Online (Sandbox Code Playgroud) 我希望能够在我的Spring应用程序中创建一个基本控制器,除其他外,它确定用户是否是注册用户.遵循模板设计模式的此基本控制器将包含控制器子类将实现的抽象受保护方法.
抽象方法会将一个User实例传递给它,注册或以其他方式.但是,我不知道如何做到这一点,因为通过使用纯粹使用@Controller注释的控制器,每个控制器都可以自由地定义他们的请求处理方法.
是否会创建一种注入每个控制器并用于验证用户的某种用户服务类是解决此问题的一种方法?这引出了一个问题(至少对我而言)这样的控制器是如何获得HttpServletRequest或Session对象的?
谢谢.
我有以下课程:
在Project模型中,我添加了以下语句:
has_and_belongs_to_many :people
accepts_nested_attributes_for :people
Run Code Online (Sandbox Code Playgroud)
当然还有课堂上适当的陈述Person.如何通过方法添加Developer到a ?以下不起作用:Projectnested_attributes
@p.people_attributes = [{:name => "Epic Beard Man", :type => "Developer"}]
@p.people
=> [#<Person id: nil, name: "Epic Beard Man", type: nil>]
Run Code Online (Sandbox Code Playgroud)
如您所见,type属性设置为nil而不是"Developer".
inheritance ruby-on-rails single-table-inheritance nested-attributes
比如说,如果我们生成了一个模型
rails generate model animal name:string birthday:date
Run Code Online (Sandbox Code Playgroud)
现在我们想要创建其他模型来继承它(比如Dog和Cat),我们应该rails generate model再次使用还是自己添加文件?如果我们使用,我们如何指定Dog应该继承Animal rails generate model?
我想如果我们使用rails generate model而不是自己添加模型文件,那么也会为我们创建单元测试文件和fixture文件.还添加了迁移文件,除非它使用MongoDB,否则将不会有迁移文件.
inheritance ruby-on-rails single-table-inheritance rails-models
我有一个模特
class Post
include Mongoid::Document
include Mongoid::Timestamps
embeds_one :comment
end
Run Code Online (Sandbox Code Playgroud)
我有评论课
class Comment
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :post
field :title
field :description
end
Run Code Online (Sandbox Code Playgroud)
我有另一个继承自评论的类
class RecentComment < Comment
# certain methods
end
Run Code Online (Sandbox Code Playgroud)
现在,我希望能够创建RecentComment经过post,如果我做Post.last.build_comment(:_type => "RecentComment")了新的注释不会的_type:"RecentComment",同样如果我这样做Post.last.build_recent_comment,它给了我错误说某事像undefined method build_recent_comment for Post class.如果post有references_many :comments我应该做的Post.last.build_comments({}, RecentComment),没有任何问题.但RecentComment在这种情况下,我不知道如何使用类构建对象.如果有人可以帮助那就是gr8!
注意:我正在使用 gem 'mongoid', '~> 2.0.1'
尝试使用简单限制装饰的hibernate Criteria从数据库列出对象时出现此错误
Criteria criteria = session.createCriteria(Licence.class);
criteria.add(Restrictions.eq("gym", gym.getId()));
List<Licence> list = criteria.list();
Run Code Online (Sandbox Code Playgroud)
我有两个类:Licence有一个关联Gym.这两个类正在扩展DataModel,用于管理有关数据编辑的信息 - (创建和提供,谁和何时).这两个班级的重要性也很重要@Inheritance(strategy = InheritanceType.SINGLE_TABLE).
执照
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Licence extends DataModel implements Serializable {
private Gym gym;
private String licenceType;
private String keyCode;
private Date expireDate;
private ELicenceExpiry expired;
public Licence() {
}
@ManyToOne
@JoinColumn(name="gym_id")
public Gym getGym() {
return gym;
}
public void setGym(Gym gym) {
this.gym = gym;
}
@Column(name = "licence_type")
public String …Run Code Online (Sandbox Code Playgroud) 在我的数据库中,我有一个表people,我正在使用单表继承,这些类:
class Person < ActiveRecord::Base
end
class Member < Person
end
class Business < Member
end
Run Code Online (Sandbox Code Playgroud)
它产生的查询让我很困惑.我想要的是Member.all返回所有企业以及会员的任何其他子类型.这是什么,但只有我最近访问过Business类.我认为这是因为我的类没有被缓存在开发模式中(出于显而易见的原因),但它仍然看起来像奇怪/错误的行为.
这是rails中的错误吗?还是按预期工作?在任何一种情况下,任何人都可以想到一个很好的发展目的吗?
我有一个名为users的基表,它包含有关用户的所有常见信息,如姓名,地址,电话号码等
我有另一个名为clients的表,它包含有关客户端的特定信息(例如客户端的公司名称及其URL),并从users表继承用户信息.客户端具有外键user_id,其映射回关于用户的信息.
我有另一个名为client_admins的表,它包含有关client_admins的特定信息,还有一个user_id字段和一个client_id字段(链接到clients表).
我有另一个名为super_admins的表,它链接到users表并具有关于超级管理员的特定信息.
我知道我可能会逃脱单表继承,因为每种类型之间没有很多不同的数据,只是不同的功能和特权.
在Rails 3中对此进行建模的最佳方法是什么?
inheritance ×3
activerecord ×1
annotations ×1
controller ×1
criteria ×1
doctrine-orm ×1
hibernate ×1
java ×1
models ×1
mongoid ×1
php ×1
rails-models ×1
ruby ×1
ruby-1.9.2 ×1
spring ×1