Search code examples
rubysinatra-activerecord

set likes based on 2 parameters


I'm working on a project which should be able to put likes on it... one from each user that log into the page. I'm using ruby, active record and sinatra.

post '/like/:isbn' do
   if favourite = Favourite.find_by(book_id: params[:book_id])
    redirect to "/info/#{ params[:isbn] }"
else
  favourite = Favourite.new
  favourite.book_id = params[:book_id]
  favourite.user_id = current_user.id
  favourite.save
  redirect to "/info/#{ params[:isbn] }"
end

end what I'm trying to do is searching for this favourite where my book_id = book_id && user_id = user_id... but I can't think of a way of putting it into ruby code....


Solution

  • why don't you try this

    Favourite.where(:my_book_id=> book_id , :user_id=> user_id)
    

    this will return the array of favourite which has both :my_boo_id = book_id and :user_id = user_id