gpt4 book ai didi

mysql - Rails first_or_create!在数据库表中创建空值

转载 作者:行者123 更新时间:2023-11-29 02:56:48 37 4
gpt4 key购买 nike

我正在使用 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", "lisa@holy.com"], ["community_id", 1]]
SQL (0.3ms) INSERT INTO "members" ("email", "community_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "lisa@holy.com"], ["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 %>

似乎我在这里遗漏了一些明显的东西。有任何想法吗?谢谢。

最佳答案

我想您会希望通过唯一属性、电子邮件来查找,并通过社区名称创建。

如果是这种情况,您必须执行以下操作:

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

关于mysql - Rails first_or_create!在数据库表中创建空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29972968/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com