ArgumentError: wrong number of arguments (given 1,

2019-08-28 20:20发布

I am creating a recurring event app where certain users (streamers) can schedule recurring weekly events (streams) and other users (viewers) can follow them. The result is a personalized weekly calendar detailing when all followed streamers' events begin and end.

enter image description here

However, because viewers can follow an infinite number of streamers and therefore the resulting calendar would look like a hot mess. So I added a boolean attribute to the relationships table that indicates whether the relationship has been favorited.

  create_table "relationships", force: :cascade do |t|
    t.integer "follower_id"
    t.integer "followed_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "favorited", default: false
    t.index ["followed_id"], name: "index_relationships_on_followed_id"
    t.index ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
    t.index ["follower_id"], name: "index_relationships_on_follower_id"
  end

This way, viewers will have a second personalized calendar that will show only favorited streamers' events.

I already have a follow and unfollow method that successfully create and destroy a relationship between viewer and streamer, but I'm having trouble successfully updating an existing relationship from unfavorited to favorited.

test "should favorite and unfavorite a streamer" do
    yennifer = users(:yennifer)
    cirilla = users(:cirilla)
    yennifer.follow(cirilla)
    yennifer.favorite(cirilla)     #user_test.rb:151
end

The testing suite returns the following error and I can't figure out what the missing argument is.

["test_should_favorite_and_unfavorite_a_streamer", #<Minitest::Reporters::Suite:0x0000000006125da8 @name="UserTest">, 0.16871719993650913]
 test_should_favorite_and_unfavorite_a_streamer#UserTest (0.17s)
ArgumentError:         ArgumentError: wrong number of arguments (given 1, expected 2)
            app/models/relationship.rb:11:in `favorite'
            app/models/user.rb:124:in `favorite'
            test/models/user_test.rb:151:in `block in <class:UserTest>'

  27/27: [=================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.82473s
27 tests, 71 assertions, 0 failures, 1 errors, 0 skips

User.rb

# Follows a user
def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
end

# Unfollows a user
def unfollow(other_user)
    active_relationships.find_by(followed_id: other_user.id).destroy
end

def favorite(other_user)
    active_relationships.find_by(followed_id: other_user.id).favorite     #user.rb:124
end

def unfavorite(other_user)
    active_relationships.find_by(followed_id: other_user.id).unfavorite
end

Relationships.rb

def favorite
    update_attribute(favorited: true)     #relationship.rb:11
end

def unfavorite
    update_attribute(favorited: false)
end

Can someone please help me in identifying the missing argument and fixing this problem. Thank you.

1条回答
干净又极端
2楼-- · 2019-08-28 20:54

The first comment on this answer is correct, so I don't know why this person posted as a comment rather than an answer. update_attribute takes two arguments and you are passing it a single hash argument. Your "favorite" method is the equivalent of update_attribute({favorited: true}) when you really want update_attribute(:favorited, true)

查看更多
登录 后发表回答