ruby on rails - wrong number of arguments (given 0, expected 1..2) - Stack Overflow

admin2025-04-21  0

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

Share Improve this question edited Feb 3 at 14:05 lmtaq 5204 silver badges23 bronze badges asked Feb 3 at 3:03 David BistaDavid Bista 411 silver badge2 bronze badges 1
  • I think it should be like this: attendances.exists?(attended_activity: activity, status: Attendance.statuses[status]) – Ankit Pandey Commented Feb 5 at 6:41
Add a comment  | 

1 Answer 1

Reset to default 8

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745251010a292505.html

最新回复(0)