本文介绍了Rails STI和“类型”的设置。串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我需要在Rails中使用STI。

I think I need to use STI in Rails.

这是我的课程:

class Person < ActiveRecord::Base
end

class Landlord < Person
end

并且人员表有一个:type 这是一个字符串的列。

and the people table has a :type column that's a string.

所以,我期望在表中看到的是,作为Person的每一行都有类型集人和每个房东的类型设置为房东。但是,这不是我所看到的。每个房东的类型都设置为Landlord,但所有Person的类型都设置为nil。这很好可能是rails的工作方式,但我只是在寻找一些确认。

So, what I expect is to see in the table, is that every row that is a Person has the type set to "Person" and every Landlord has the type set to "Landlord". However, that's not what I see. Each Landlord has the type set to "Landlord", but all the Person have their type set to nil. This very well could be the way that rails works, but I was just looking for some confirmation.

推荐答案

这确实是STI的工作原理。基类在数据库中的类型为NULL(或者在ruby中为nil)。

This is, indeed, how STI works. The base class has a type of NULL in the database (or nil in ruby).

这篇关于Rails STI和“类型”的设置。串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:51