From becc66f76918855deccc8056294e3f6bff51750e Mon Sep 17 00:00:00 2001 From: BoHung Chiu Date: Sat, 1 Jul 2023 15:35:21 +0800 Subject: [PATCH] Fix bug. --- app/models/journal_paper.rb | 49 +++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/app/models/journal_paper.rb b/app/models/journal_paper.rb index 70105ae..c833581 100644 --- a/app/models/journal_paper.rb +++ b/app/models/journal_paper.rb @@ -31,7 +31,7 @@ class JournalPaper # paginates_per 10 #relations - has_and_belongs_to_many :member_profile, class_name: "MemberProfile", foreign_key: 'member_profile_id' + field :member_profile_id, type: Array belongs_to :journal_paper_type has_and_belongs_to_many :journal_types @@ -54,9 +54,23 @@ class JournalPaper scope :sort_year_date, ->{ order_by(:year => "desc", :publication_date => "desc", :id=>"desc") } scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "desc", :id=>"desc") } - alias_method :member_profiles, :member_profile - alias_method :member_profiles=, :member_profile= - alias_method :org_member_profile=, :member_profile= + before_save :fix_member_profile_id + after_save :reload_member_profiles + + def fix_member_profile_id + org_member_profile_id = self[:member_profile_id] + if org_member_profile_id.class == Array + self.member_profile_id = self[:member_profile_id].map{|id| BSON::ObjectId(id)} + else + self.member_profile_id = Array(org_member_profile_id).compact.map{|id| BSON::ObjectId(id)} + end + true + end + def reload_member_profiles + @member_profiles = MemberProfile.where(:id.in=>self.member_profile_id).to_a + @member_profile = @member_profiles.first + return @member_profiles + end def create_link title = [] @@ -215,12 +229,33 @@ class JournalPaper plugin_datas end - def member_profile() - member_profiles.first + def member_profiles + @member_profiles ||= MemberProfile.where(:id.in=>self.member_profile_id).to_a + @member_profiles + end + + def member_profiles=(mps) + self.member_profile_id = mps.map(&:id) + @member_profiles = mps + @member_profile = @member_profiles.first + @member_profiles + end + + def member_profile + @member_profile ||= MemberProfile.where(:id.in=>self.member_profile_id).first + @member_profile end def member_profile=(mp) - org_member_profile=(mp.class != MemberProfile ? mp : [mp]) + if mp.class == MemberProfile + self.member_profile_id = [mp.id] + @member_profiles = [mp] + @member_profile = mp + else + self.member_profile_id = mp.map(&:id) + @member_profiles = mp + @member_profile = mp.first + end end def get_plugin_field_data(field)