Tag: 单表inheritance

Rails STI:如何更改“类型”列的类名和值之间的映射

由于公司规则,我不能使用我们的域名类; 我打算用一个类比来代替。 我有一个名为projects的表,其中有一个名为’type’的列,可能的值为’indoor’和’outdoor’。 具有室内和室外的记录具有明显的function分离,并且作为STI实现非常巧妙地适合。 不幸的是,我无法更改类型名称,也无法在全局命名空间中添加类。 有没有办法为’type’指定不同的值? 注意:我不是要为STI使用不同的列名而不是’type’。 我希望类型的值不同于我的类名。

Rails:使用具有单表inheritance的Devise

我遇到了一个问题,就是让Devise按照我对单表inheritance的方式工作。 我有两种不同类型的帐户,如下所示: class Account < ActiveRecord::Base devise :database_authenticatable, :registerable end class User < Account end class Company < Account end 我有以下路线: devise_for :account, :user, :company 用户在/user/sign_up注册, /company/sign_up在/company/sign_up注册。 所有用户使用/account/sign_in的单个表单/account/sign_in ( Account是父类)。 但是,通过此表单登录似乎只是为Account范围validation它们。 对/user/edit或/company/edit等操作的后续请求将用户指向相应范围的登录屏幕。 如何让Devise识别帐户“类型”并对相关范围进行身份validation? 任何建议非常感谢。

Rails STI和has_many通过关联不起作用,SQLException:没有这样的表

我有以下型号: class Test < ApplicationRecord end class Exam < Test end class Practice < Test has_many :relations has_many :questions, through: :relations end class Relation < ApplicationRecord belongs_to :practice belongs_to :question end class Question < ApplicationRecord has_many :relations has_many :practices, through: :relations end 这是我的架构: create_table “questions”, force: :cascade do |t| t.string “title” t.text “text” t.datetime “created_at”, […]

在单表inheritance机制中使用动态创建的类

我有一个名为’DynObject’的ActiveRecord类,可用于inheritance。 在初始化时,我动态创建一些从它inheritance的类: classes_config = { foo: ‘foo’, bar: ‘bar’ } classes_config.each do |name,options| klass = Class.new( DynObject ) do end self.klasses[name] = const_set( “#{name.camelize}DynObject”, klass ) end 这一切都很好,这些类创建得很好..但是当ActiveRecord尝试加载创建的记录时,STI机制失效.. (ActiveRecord::SubclassNotFound (The single-table inheritance mechanism failed to locate the subclass: ‘FooObject’….)) 我认为这很奇怪,因为当我检查类在type列中它们的命名方式时,它们存在… 当我检查这些类的ancestors ,他们也inheritance了… 有可能我想要完成的事情吗? 还有什么需要做的吗?

Rails:构建涉及多态关联和STI的查询

我正在尝试找到关于照片的10条最新评论,因此我可以将它们集成到我的Rails 3.0.3应用程序的活动源中。 我有一个Photo模型,它inheritance了使用单表inheritance的Upload模型: class Upload < ActiveRecord::Base … end class Photo :commentable … end 可评论的多态关联在Comment模型中描述: class Comment true end 到目前为止一切都那么好吧? 当我尝试构建查询时出现问题。 经过一些试验和错误,我想出了这个代码,它位于Photo模型中: def self.latest_comments(count = 10) Comment.where(:commentable_type => “Upload”)\ .joins(“INNER JOIN uploads ON comments.commentable_id = uploads.id”)\ .where(“uploads.type” => “Photo”).order(“comments.created_at DESC”).limit(count) end 这段代码适用于我的SQLite开发机器,但是我必须做一些修改才能让它在使用PostgreSQL的生产服务器上运行。 上面的查询化身尚未在生产服务器上进行过测试,但我一直在寻找一种更简洁的结构化查询方式,因为上面的内容并不十分健壮,也不像“Railsy”。 我想要的是能够说的是 Comment.joins(:commentable => :photo) …但是这引发了一个exception,大概是因为Rails不知道如何进行连接: ActiveRecord :: EagerLoadPolymorphicError:无法急切加载多态关联:commentable 我在StackOverflow上发表了一篇文章 ,描述了一种查询多态关联的不同方式。 我能够根据这篇文章提出以下代码: Comment.find_all_by_commentable_type(“Upload”, […]

DataMapper – 单表inheritance

有人可以向我解释这里发生了什么吗? 这是一个我放在一起展示你们最新情况的例子: class Person include DataMapper::Resource property :id, Serial property :type, Discriminator property :name, String property :age, Integer end class Male < Person end class Father < Male property :job, String end class Son < Male end class Female < Person end class Mother < Female property :favorite_song, String end class Daughter < Female end […]

实现单表inheritance后损坏的Rails路由

我已经为person类实现了单表inheritance class Person < ActiveRecord::Base end class Teacher < Person end class Student < Person end class Outsider < Person end 创建人员似乎根据form.select中选择的内容创建了Teacher,Student或Person,并添加了type属性。 但是,我似乎打破了路线 | 它们似乎指向teacher_path,student_path和outsider_path而不是person_path。 路线需要做哪些改变?

当有STI时,Rails设计为注册表单添加字段

这是我的模特: class User ‘Worker’, :foreign_key => :worker_id devise :database_authenticatable accepts_nested_attributes_for :worker attr_accessible :worker_id, :email, :password, :password_confirmation, :remember_me, :workers_attributes, :worker_attributes, :name, :worker end class Worker < User devise :database_authenticatable, :registerable belongs_to :user attr_accessible :name, :worker, :workers end 我正在尝试将字段名称添加到http:// localhost:3000 / workers / sign_up的注册表单中 注册表格 Create Worker resource_name, :url => registration_path(resource_name) do |f| %> Name: “devise/shared/links” %> […]

使用单表inheritance更改Rails中的ActiveRecord类的类型

我有两种类: BaseUser < ActiveRecord::Base 和 User < BaseUser 其中acts_as_authentic使用Authlogic的身份validation系统。 此inheritance是使用单表inheritance实现的 如果新用户注册,我将他注册为用户。 但是,如果我已经有一个具有相同电子邮件的BaseUser,我想将该BaseUser更改为数据库中的用户,而不是简单地将所有数据从BaseUser复制到用户并创建新用户(即使用新用户) ID)。 这可能吗? 谢谢。

单表inheritance用于引用具有自己的字段的子类

我正在使用Ruby on Rails 3,我实现了一个有效的单表inheritance,如下所示: class User < ActiveRecord::Base # Schema Information # # Table name: User # # id : integer # type : string # children_user_id: integer … end class UserAdmin < User # Schema Information # # Table name: UserAdmin # # id : integer # special_field1 : string # special_field2 : string […]