删除组中的单个用户 - Active Record

时间:2016-03-31 14:38:04

标签: ruby-on-rails ruby activerecord

我有两个模型GroupPerson。两者都是HABTM,但我希望一个人能够在不删除该组的情况下退出特定组,而只是该组中的人。我一直在尝试在控制台中解决这个问题,我似乎无法得到它。我将展示我的代码以便澄清。

我目前正在制作此方法

def delete_from_group(phone_number)
 person = Person.find_by(phone_number: phone_number)
 person.groups.destroy
end

这是我的控制者。

def create
# Grab the phone number from incoming Twilio params
@phone_number = params[:From]
# Find the subscriber associated with this number or create a new one
@subscriber = Person.find_or_create_by(phone_number: @phone_number)
@delete_group = Person.find_by(phone_number: @phone_number)

# Update location data
@subscriber.update(
  city: params[:FromCity],
  state: params[:FromState],
  zip: params[:FromZip],
  country: params[:FromCountry]
)

@body = params[:Body].to_s.downcase.strip
begin
  # Process the command from our Subscriber
  output = process_message(@body, @subscriber, @delete_group)
rescue
  output = "Something went wrong. Try again."
end

# Render the TwiML response

respond(output)
end

private

def process_message(message, subscriber, delete_group)
if worker_groups.include?(message)
  subscriber.update(subscribed: true)
  subscriber.add_to_group(message)
  "You have been subscribed to the #{message.capitalize} list"

elsif message == "stop volunteer"
  delete_group.delete_from_group("volunteer")
  "You have been unsubscribed from the specified list"
elsif message == "stop dancer"
  delete_group.delete_from_group("dancer")
  "You have been unsubscribed from dancer list"
elsif message == "stop staff"
  delete_group.delete_from_group("staff")
  "you have been unsubscribed from staff list"

elsif message == "tulip" || message == "stem"
  subscriber.update(subscribed: message == "tulip")
  subscriber.add_to_group("visitor")

  if subscriber.subscribed
    "You are now subscribed for updates."
  else
    "You have unsubscribed from notifications. Text 'TULIP' to start receieving updates again."
  end
else
  "Sorry, we don't recognize that command. Available commands are: 'TULIP' or 'STEM'."
end
end

def worker_groups
%w(dancer staff volunteer)
end

def worker_groups
 %w(dancer staff volunteer)
end

3 个答案:

答案 0 :(得分:7)

如果您要将组ID作为变量group_id删除:

 person = Person.find_by(phone_number: phone_number)
 group = person.groups.find(group_id)

 if group
    person.groups.delete(group)
 end

请参阅this awesome post on the subject

另见Rails documentation

  

collection.delete(object,...)从中删除一个或多个对象   通过从连接表中删除它们的关联来进行收集。这个   不破坏物体。

答案 1 :(得分:3)

我会使用has_many :through代替HABTM。这使您可以直接删除连接表行,也可以在连接表上添加元数据。

如果您有传统设置,只需重命名表格并更改模型上的关系定义。

class RenameGroupsPersons < ActiveRecord::Migration
  def change
    rename_table :groups_people, :memberships
  end
end

由于ActiveRecord从表中解析类名的方式,因此需要重命名。如果表名为Groups::People,ActiveRecord将尝试加载groups_peopleGroupPeople只是奇怪,所以请放手Membership

class Person < ActiveRecord::Base
  has_many :memberships
  has_many :groups, through: :memberships
end

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :people, through: :memberships
end

class Membership
  belongs_to :group
  belongs_to :person
  validates_uniqueness_of :group_id, scope: :person_id
end

您现在可以直接从连接模型关联中删除行:

person.memberships.find_by(group_id: 5).destroy

答案 2 :(得分:2)

您可以通过以下方式删除Group关联:

person.groups = []
person.save

或者:

person.update(groups: [])