单表继承:层次结构中的所有类都必须具有相同的属性吗?
我的
class Item < ActiveRecord::Base
end
class Talk < Item
end
迁移
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.string :type
t.string :name
t.text :description
t.time :start_time
t.time :duration
t.timestamps
end
end
...
end
有以下内容默认情况下,描述属性将在 Item 和 Talk 类上可用。有没有办法限制该属性,使其仅适用于 Talk 类?
I have the following
class Item < ActiveRecord::Base
end
class Talk < Item
end
with the migration
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.string :type
t.string :name
t.text :description
t.time :start_time
t.time :duration
t.timestamps
end
end
...
end
By default the description property will be available on the Item and Talk classes. Is there a way to restrict the property so that is only available to the Talk class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你总是可以这样做,但这是徒劳的大量工作。当您读取某个项目的持续时间时,最糟糕的情况是什么?您将返回 nil,这将导致不久后崩溃。在 Ruby 中您不需要如此担心这些类型的问题。
如果需要,您可以创建一个模块并将该模块包含在两个类中以实现共享行为,然后删除 STI。
You could always do that, but it's a lot of work for nothing. What's the worst that will happen when you read duration on an Item? You'll get back nil, which will cause a crash shortly thereafter. You don't need to be so concerned about these types of issues in Ruby.
If needed, you can create a module and include the module in the two classes, for the shared behavior, and drop the STI.