I am trying to implement a feature of join event and cancel event. I have already created attendances table and included status as an attribute. The relation is many to many, so in my attendance model the code looks like this:
class Attendance < ApplicationRecord
belongs_to :attendee, class_name: "User", foreign_key: "attendee_id"
belongs_to :attended_activity, class_name: "Activity", foreign_key: "attended_activity_id"
enum status: { joined: 0, canceled: 1 }
end
How I am showing it in the show view page:
<% if current_user.attending?(@activitie, status: "joined") %>
<%= button_to "Leave Event", activity_attendance_path(@activitie), method: :delete, class: "btn btn-danger" %>
<% else %>
<%= button_to "Attend", activity_attendances_path(@activitie), method: :post, class: "btn btn-success" %>
<% end %>
The attending method:
def attending?(activity, status: "joined")
attendances.exists?(attended_activities: activity, status: status)
end
I guess it is throwing the error in the attending method, but when I used byebug and called attendances I kept getting ArgumentError (wrong number of arguments (given 0, expected 1..2))
indicating enum which is in the Attendance model
I am trying to implement a feature of join event and cancel event. I have already created attendances table and included status as an attribute. The relation is many to many, so in my attendance model the code looks like this:
class Attendance < ApplicationRecord
belongs_to :attendee, class_name: "User", foreign_key: "attendee_id"
belongs_to :attended_activity, class_name: "Activity", foreign_key: "attended_activity_id"
enum status: { joined: 0, canceled: 1 }
end
How I am showing it in the show view page:
<% if current_user.attending?(@activitie, status: "joined") %>
<%= button_to "Leave Event", activity_attendance_path(@activitie), method: :delete, class: "btn btn-danger" %>
<% else %>
<%= button_to "Attend", activity_attendances_path(@activitie), method: :post, class: "btn btn-success" %>
<% end %>
The attending method:
def attending?(activity, status: "joined")
attendances.exists?(attended_activities: activity, status: status)
end
I guess it is throwing the error in the attending method, but when I used byebug and called attendances I kept getting ArgumentError (wrong number of arguments (given 0, expected 1..2))
indicating enum which is in the Attendance model
The enum
class method changed its signature between Ruby on Rails version 6 and 7. In earlier versions, you defined a enum
like this:
enum status: { joined: 0, canceled: 1 }
In newer versions like this:
enum :status, { joined: 0, canceled: 1 }
Or in your example, simplified like this:
enum :status, [:joined, :canceled]
See enum
in the Ruby on Rails docs.
attendances.exists?(attended_activity: activity, status: Attendance.statuses[status])
– Ankit Pandey Commented Feb 5 at 6:41