Mysql Rails优先\u或\u创建!在数据库表中创建空值
我使用first_或_create将电子邮件订阅者(称为成员)的列表填充到一个表中。代码如下:Mysql Rails优先\u或\u创建!在数据库表中创建空值,mysql,ruby-on-rails,postgresql,Mysql,Ruby On Rails,Postgresql,我使用first_或_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
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
文件:
您能卸下这台机器吗!从第一个\u或\u创建!,我猜社区成员的功能是在社区模型中。在保存社区对象之前,您是否可以在模型中创建它。我已尝试删除!这也没什么区别。社区对象在添加任何成员之前已经存在,因此我认为不需要保存它?是的,我需要它是社区独有的。所以我可以有相同的电子邮件地址,但一个是community_id=1,另一个是community_id=2。关于如何使这项工作正常进行,有什么想法吗?好的,让我们考虑一下“helper”表的选项。我们可以创建一个
member\u社区
表吗?然后我们创建唯一的“助手”记录,您可以依赖这些记录进行关联。如果你需要帮助,我可以写下来。你能详细解释一下吗?我有一个包含email和community_id列的members表。我对rails和数据库非常陌生,所以任何细节都会有很大帮助。谢谢。好的,我知道你要说什么了。我使用在我的应用程序的不同部分中设置的类似联接表。但我如何更新记录以添加新电子邮件并确保社区内的唯一性,而不是整个应用程序?我已经更新了这个问题,所以你可以看到我是如何调用community_member方法的。谢谢你帮了我这个忙。乔-是的!这很有魅力。非常感谢。你能给我解释一下使用map和只是重复浏览拆分邮件列表的区别吗?