2015-04-30 48 views
2

我正在使用first_or_create填充電子郵件訂戶(稱爲成員)列表的表。代碼如下:Rails first_or_create!在數據庫表中創建空值

def community_members=(members) 
     self.members = members.split(",").map do |member| 
      Member.where(email: member.strip, community_id: self.id).first_or_create! unless member.strip == nil 
     end 
    end 

一切正常,只是當我添加額外的電子郵件,以同一社區,表變成了「community_id」列所有以前的行爲NULL。

這裏的服務器日誌:

Member Load (0.2ms) SELECT "members".* FROM "members" WHERE "members"."email" = $1 AND "members"."community_id" = $2 ORDER BY "members"."id" ASC LIMIT 1 [["email", "[email protected]"], ["community_id", 1]] 
    SQL (0.3ms) INSERT INTO "members" ("email", "community_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "[email protected]"], ["community_id", 1], ["created_at", "2015-04-30 16:14:25.930012"], ["updated_at", "2015-04-30 16:14:25.930012"]] 

    Member Load (0.2ms) SELECT "members".* FROM "members" WHERE "members"."community_id" = $1 [["community_id", 1]] 
    SQL (0.4ms) UPDATE "members" SET "community_id" = NULL WHERE "members"."community_id" = $1 AND "members"."id" = 30 [["community_id", 1]] 
    (0.3ms) COMMIT 

第一個「會員」負荷不正是它應該做的。但由於某種原因,它總是以第二個成員加載結束,並將所有「community_id」字段設置爲NULL。

現在我打電話:community_member從形式的社區頁面上:

<%= form_for(@community) do |f| %> 
     <%= f.text_area :community_members, class:"form-control input-lg", placeholder:"Please add your list of comma separated member email addresses here" %> 
     <%= f.submit "Save", class: "btn btn-lg btn-green btn-block pad-top" %> 
    <% end %> 

好像我缺少明顯的東西在這裏。有任何想法嗎?謝謝。

+0

你可以刪除!從first_or_create !,我想community_members函數是在社區模型中。你可以在保存社區對象之前在模型中創建它。 – coderhs

+0

我嘗試刪除!並沒有什麼區別。在添加任何成員之前,社區對象已經存在,所以我認爲它不需要保存? – Lorenz

回答

1

我想,您將希望通過獨特的屬性,電子郵件和社區名稱創建。

如果是這樣的話,你就必須這樣做:如果你有一個非唯一的電子郵件記錄

Member.where(email: member.strip).first_or_create(community: self) unless... 

,你就必須重新設計協會。

class Subscriber 
    #this should have the email attribute 
    has_many :community_subscribers 
    has_many :communities, through: :community_subscribers 
end 

class CommunitySubscriber 
    #this is a 'bridge' table 
    belongs_to :subscriber 
    belongs_to :community 
end 

class Community 
    has_many :community_subscribers 
    has_may :subscribers, through: :community_subscribers 

    #I suggest new method and arg names 
    #Using self will keep the query within the scope of the community you are working on 
    #This also allows for creation of Subscriber records if you insist placing that here 
    #are you sure you want to map instead of just iterating the split list of emails? 
    def new_subscribers(emails) 
    emails.split(",").map do |email| 
     clean_email = email.strip 
     subscriber = Subscriber.where(email: clean_email).find_or_create unless clean_email.blank? 
     self.community_subscribers.where(subscriber: subscriber).first_or_create unless subscriber.blank? 
    end 
end 
end 

文檔: http://apidock.com/rails/v3.2.1/ActiveRecord/Relation/first_or_create http://guides.rubyonrails.org/v3.2.13/active_record_querying.html#first_or_create

+0

是的,我需要它是社區獨一無二的。因此,我可以使用相同的電子郵件地址,但其中一個使用community_id = 1,另一個使用community_id = 2.有關我如何使其工作的任何想法? – Lorenz

+0

好吧,讓我們考慮一下'helper'表的選項。我們可以做些什麼比如創建一個'member_community'表嗎?然後,我們創建獨一無二的「幫手」記錄,您可以依賴這些關聯。如果你需要幫助,我可以寫出來。 –

+0

你能詳細解釋一下嗎?我有一個列電子郵件和community_id成員表。我對軌道和數據庫很熟悉,所以任何細節都會有很大的幫助。謝謝。 – Lorenz