修改开源项目社区中 每个项目的结构, 不再是一对一的匹配帖子,而是添加了一个中间表

This commit is contained in:
fanqiang 2014-05-20 10:06:59 +08:00
parent 543a199c5f
commit 87a6dd4938
4 changed files with 20 additions and 7 deletions

View File

@ -3,8 +3,10 @@ class OpenSourceProject < ActiveRecord::Base
include Redmine::SafeAttributes
has_many :applies, :class_name => "ApplyProjectMaster", :as => :apply, :dependent => :delete_all
has_many :topics, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :conditions => "#{RelativeMemo.table_name}.parent_id IS NULL", :order => "#{RelativeMemo.table_name}.created_at DESC", :dependent => :destroy
has_many :relative_memos, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :dependent => :destroy
has_many :relation_topics, :class_name => 'RelativeMemoToOpenSourceProject', :foreign_key => 'osp_id', :order => "#{RelativeMemo.table_name}.created_at DESC", :dependent => :destroy
# has_many :topics, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :conditions => "#{RelativeMemo.table_name}.parent_id IS NULL", :order => "#{RelativeMemo.table_name}.created_at DESC", :dependent => :destroy
has_many :topics, :through => :relation_topics, :class_name => 'RelativeMemo'
# has_many :relative_memos, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :dependent => :destroy
has_many :tags, :through => :project_tags, :class_name => 'Tag'
has_many :project_tags, :class_name => 'ProjectTags'
has_many :masters, :class_name => 'ApplyProjectMaster', :as => :apply, :dependent => :delete_all, :conditions => "#{ApplyProjectMaster.table_name}.status = 2"
@ -126,9 +128,14 @@ class OpenSourceProject < ActiveRecord::Base
def self.reset_counters!(id)
osp_id = id.to_i
update_all("topic_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NULL)," +
" memo_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NOT NULL)," +
" last_memo_id = (SELECT MAX(id) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id})",
# update_all("topic_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NULL)," +
# " memo_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NOT NULL)," +
# " last_memo_id = (SELECT MAX(id) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id})",
# ["id = ?", osp_id])
update_all("topic_count = (SELECT COUNT(*) FROM #{RelativeMemoToOpenSourceProject.table_name} WHERE osp_id=#{osp_id})",
# +
# " memo_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NOT NULL)," +
# " last_memo_id = (SELECT MAX(id) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id})",
["id = ?", osp_id])
end
end

View File

@ -7,6 +7,8 @@ class RelativeMemo < ActiveRecord::Base
has_many :tags, :through => :project_tags, :class_name => 'Tag'
has_many :project_tags, :class_name => 'ProjectTags'
has_many :relation_topics, :class_name => 'RelativeMemoToOpenSourceProject'
has_many :no_uses, :as => :no_use, :dependent => :delete_all
acts_as_taggable

View File

@ -1,3 +1,7 @@
class RelativeMemoToOpenSourceProject < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :open_source_project, :foreign_key => "osp_id"
belongs_to :relative_memo, :foreign_key => 'relative_memo_id'
validates_presence_of :osp_id, :relative_memo_id
end

View File

@ -1,10 +1,10 @@
class CreateRelativeMemos < ActiveRecord::Migration
def change
create_table :relative_memos do |t|
t.integer :osp_id, :null => false
t.integer :osp_id, :null => true
t.integer :parent_id, null: true
t.string :subject, null: false
t.text :content, null: false
t.mediumtext :content, null: false
t.integer :author_id
t.integer :replies_count, default: 0
t.integer :last_reply_id