From 87a6dd4938dc8f6dffd7219827eae566d0e95c2f Mon Sep 17 00:00:00 2001 From: fanqiang <316257774@qq.com> Date: Tue, 20 May 2014 10:06:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=80=E6=BA=90=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=A4=BE=E5=8C=BA=E4=B8=AD=20=E6=AF=8F=E4=B8=AA?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=9A=84=E7=BB=93=E6=9E=84=EF=BC=8C=20?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E6=98=AF=E4=B8=80=E5=AF=B9=E4=B8=80=E7=9A=84?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=B8=96=E5=AD=90=EF=BC=8C=E8=80=8C=E6=98=AF?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80=E4=B8=AA=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/open_source_project.rb | 17 ++++++++++++----- app/models/relative_memo.rb | 2 ++ .../relative_memo_to_open_source_project.rb | 4 ++++ .../20140401004102_create_relative_memos.rb | 4 ++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/models/open_source_project.rb b/app/models/open_source_project.rb index e526dc921..6413a0d9f 100644 --- a/app/models/open_source_project.rb +++ b/app/models/open_source_project.rb @@ -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 diff --git a/app/models/relative_memo.rb b/app/models/relative_memo.rb index ba7974da3..9f88c4c86 100644 --- a/app/models/relative_memo.rb +++ b/app/models/relative_memo.rb @@ -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 diff --git a/app/models/relative_memo_to_open_source_project.rb b/app/models/relative_memo_to_open_source_project.rb index 5dd00338c..2586d1c8e 100644 --- a/app/models/relative_memo_to_open_source_project.rb +++ b/app/models/relative_memo_to_open_source_project.rb @@ -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 diff --git a/db/migrate/20140401004102_create_relative_memos.rb b/db/migrate/20140401004102_create_relative_memos.rb index 6654b8fe5..6f0d776db 100644 --- a/db/migrate/20140401004102_create_relative_memos.rb +++ b/db/migrate/20140401004102_create_relative_memos.rb @@ -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