在我的Rails应用程序中,我有几个处理资产的模型(附件,图片,徽标等).我正在使用attachment_fu,到目前为止,我有3个不同的表用于在MySQL数据库中存储信息.
我想知道如果我使用STI并将所有信息放在一个表中,使用类型列并具有不同的继承类,它是否会对性能产生影响.它会更干燥,更容易维护,因为它们共享许多属性和特征.
但更快的是什么?每个表有多个表,少行或只有一个有很多行的表?或者没有任何区别?我将不得不每秒处理大量信息和许多查询.
感谢您的意见!
我有一个Model属性,它有使用STI的子类,
我希望所有人都使用相同的控制器,只有不同的视图部分取决于子类.
Property
Restaurant < Property
Landmark < Property
Run Code Online (Sandbox Code Playgroud)
除了我不确定如何辨别控制器内的子类以呈现正确的视图之外,它还可以工作.IE浏览器./餐厅工作,并去物业控制器,但我不能告诉他们,他们想要餐厅的子类?
map.resources :restaurant, :controller => :properties
map.resources :properties
Run Code Online (Sandbox Code Playgroud) routing routes ruby-on-rails subclass single-table-inheritance
是否可以覆盖此colummn的名称?我正在更改我的应用程序的某些部分以使用STI,还有其他字段正在使用中.我也希望它是整数类型.
有任何想法吗?
我刚刚开始学习rails和ruby,所以如果这太愚蠢了,请耐心等待.
我的应用程序中有几种不同的AppModule类型,它们具有不同的行为但数据类似,所以我使用单表继承来保存它们.
但是,当尝试允许用户明确选择他们想要的类型时,app_modules/new.html.erb我会收到警告WARNING: Can't mass-assign these protected attributes: type.这是相关代码:
<% form_for(@app_module) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :type %><br />
<%= f.select( :type, options_from_collection_for_select(AppModule.subclasses().map{ |c| c.name}, 'to_s', 'to_s')) %>
</p>
<%= f.submit 'Create' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我已尝试attr_accessible :type在模型文件中明确设置但它不起作用
我使用的是rails 2.3.8和ruby 1.8.7.
任何帮助非常感谢,谢谢......
我有一个STI适用于从一个ActiveRecord::Base模型继承的10个模型.
class Listing::Numeric < ActiveRecord::Base
end
class Listing::AverageDuration < Listing::Numeric
end
class Listing::TotalViews < Listing::Numeric
end
Run Code Online (Sandbox Code Playgroud)
有10个这样的模型继承自 Listing::Numeric
在rails控制台中,当我尝试.descendants或.subclasses它返回一个空数组.
Listing::Numeric.descendants
=> []
Listing::Numeric.subclasses
=> []
Run Code Online (Sandbox Code Playgroud)
理想情况下,这应该工作.
任何想法为什么它不返回预期的子类?
ruby activerecord ruby-on-rails single-table-inheritance ruby-2.0
我想在Rails 4中使用STI.我已经有了一个模型Boilerplate,现在我想从它继承一个BoilerplateOriginal和一个BoilerplateCopy模型.所以我type在表格中添加了一列:
class AddTypeToBoilerplates < ActiveRecord::Migration
def up
add_column :boilerplates, :type, :string
Boilerplate.update_all type: 'BoilerplateOriginal'
change_column :boilerplates, :type, :string, null: false
end
def down
remove_column :boilerplates, :type
end
end
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这个专栏似乎没有被Rails自动填充:
[1] a4aa2 » x = Boilerplate.new
=> #<Boilerplate:0x00000101609580> {
:id => nil,
:title => nil,
:type => nil
}
[2] a4aa2 » x.valid?
Boilerplate Exists (0.4ms) SELECT 1 AS one FROM "boilerplates" WHERE "boilerplates"."title" IS NULL LIMIT 1
=> false
[3] a4aa2 …Run Code Online (Sandbox Code Playgroud)