English 中文(简体)
在一个单元中,这种最清洁的方法是:在科学、技术和革新模式中,一直与科学、技术和创新模式挂钩?
原标题:is this the cleanest approach for :has_many association to STI models in a module?

The following work, but is the Cleanest approach?

简言之,一个项目:大任务。 任务以科学、技术和革新司为代表,任务的不同次类别是:模块(和子类):

# schema
create_table projects do |t|
  # t.integer :id  - implicitly generated
end
create_table task_bases do |t|
  # t.integer :id  - implicitly generated
  t.string    :type        # for STI support
  t.integer   :project_id
end

# file: app/models/project.rb
class Project < ActiveRecord::Base
  :has_many :tasks, :class_name => "Task::Base", :dependent => :destroy
end

# file: app/models/task.rb
module Task
  def self.table_name_prefix
    "task_"
  end
end

# file: app/models/task/base.rb
module Task
  class Base < ActiveRecord::Base
    :belongs_to :project
  end
end

# file app/models/task/priority.rb
module Task
  class Priority < Base
    :belongs_to :project
  end
end

几个问题:

  • Task::Base should never be instantiated -- we ll only use sub-classes of it. I think this means that I declare it as an abstract_class -- what s the proper way to do that?
  • Would it be better to name the STI table tasks rather than task_bases, and if so, what other changes need to happen?
  • Should I be troubled by the :class_name => "Task::Base" declaration?
问题回答

如上所述,上述工作正在进行。 但是,在你有任务的共同情况下:模块、任务:基本类别和几个任务子类别:基本,以下是清洁的:

define your STI table using the module name

create_table task_bases do |t|
  # t.integer :id  - implicitly generated
  t.string    :type        # for STI support
  t.integer   :project_id
end

create app/models/<module_name>.rb

该档案仅包括<代码><module_name>以下分标题的所有档案:

# file: app/models/task.rb
module Task
  require  task/base   # comment out unless task/base.rb must be loaded first
  Dir[Rails.root.join("app/models/task/**/*.rb").to_s].each {|f| require f}
end

create app/models/<module_name>/base.rb

此处所有子类别共同使用的任何功能。 请注意,这正是我们界定科学、技术和创新表格名称的。

# file: app/models/task/base.rb
module Task
  class Base < ActiveRecord::Base
    self.table_name =  tasks 
    # include common functionality here
  end
end

create any subclasses you need.

# file: app/models/task/batch.rb
module Task
  class Batch < Base
    # batch specific code here
  end
end
# file: app/models/task/sequential.rb
module Task
  class Sequential < Base
    # sequential specific code here
  end
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签