← Back to Upcase

Rails Fundamentals - Active Record Associations

(Upcase ) #1

This topic is for the [Active Record Associations][] exercise in the [Rails Fundamentals][] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Active Record Associations]: https://exercises.upcase.com/exercises/active-record-associations
[Rails Fundamentals]: https://thoughtbot.com/upcase/rails-fundamentals

(Flo Maldonado) #3

Spoiler Alert!!!
in welcome.html.erb I have
<%=pluralize(guestbook_entry.num_likes, ‘Like’)%>
which calls num_likes method in the guestbook_entry.rb model. This works in the website, but doesn’t work for the testing. I think the stub doesn’t have likes attached. I get this error.
Failure/Error: render
Double GuestbookEntry(id: integer, body: text, created_at: datetime, updated_at: datetime) received unexpected message :num_likes with (no args)

num_likes is
def num_likes
likes.count
end

schema:
create_table “guestbook_entries”, force: true do |t|
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table “likes”, force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "guestbook_entry_id"
end

add_index “likes”, [“guestbook_entry_id”], name: “index_likes_on_guestbook_entry_id”

Like belongs_to :guestbook_entry
and guestbook_entry has_many :likes

(Chris Toomey) #4

Hello @flocela, it looks as though your double does not have the num_likes method stubbed, so Rspec is raising an error. You should be able to fix by adding num_likes to the double roughly like:

# set a random number as the value for the `num_likes` method stub
guestbook_entry  = double(:guestbook_entry, num_likes: 4)
(Flo Maldonado) #5

Thank you for the response. I’ll look into this. I don’t really know how doubles work right now.