我有兩個模型Group
和Person
。都是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
我們需要了解如何'Person'和'Group'連接的更多信息。可能有助於顯示add_to_group正在執行的方法,或者顯示每個模型中的關聯。 – MTarantini