Ruby on rails 添加到收藏夹时,为什么在控制器中遇到SQLite3::ConstraintException?偏爱基于Hartl教程的micropost
我的最爱是基于Hartl的follow user按钮 我可以进入用户页面并喜欢一个micropost,但只要我尝试喜欢一秒钟,就会在Ruby on rails 添加到收藏夹时,为什么在控制器中遇到SQLite3::ConstraintException?偏爱基于Hartl教程的micropost,ruby-on-rails,ruby,sqlite,Ruby On Rails,Ruby,Sqlite,我的最爱是基于Hartl的follow user按钮 我可以进入用户页面并喜欢一个micropost,但只要我尝试喜欢一秒钟,就会在favoritescontroller\create中给我一个SQLite3::ConstraintException。约束条件是: UNIQUE constraint failed: favourites.favouriter_id, favourites.favourited_id. 为什么我会遇到这种限制 收藏夹管理员: class FavouritesCo
favoritescontroller\create
中给我一个SQLite3::ConstraintException
。约束条件是:
UNIQUE constraint failed: favourites.favouriter_id, favourites.favourited_id.
为什么我会遇到这种限制
收藏夹管理员:
class FavouritesController < ApplicationController
before_action :logged_in_user
def create
@micropost = Micropost.find(params[:favourited_id])
current_user.favourite(@micropost)
redirect_to user_path (current_user)
end
def destroy
@micropost = Favourite.find(params[:id]).favourited
current_user.unfavourite(@micropost)
redirect_to user_path (current_user)
end
end
def favouriting
@title = "Favourites"
@user = User.find(params[:id])
@microposts = @user.favouriting.paginate(page: params[:page])
render 'microposts/show_favourite'
end
def favouriter
@title = "Favourite"
@micropost = Micropost.find(params[:id])
@users = @micropost.favouriter.paginate(page: params[:page])
render 'microposts/show_favourite'
end
用户控制器:
class FavouritesController < ApplicationController
before_action :logged_in_user
def create
@micropost = Micropost.find(params[:favourited_id])
current_user.favourite(@micropost)
redirect_to user_path (current_user)
end
def destroy
@micropost = Favourite.find(params[:id]).favourited
current_user.unfavourite(@micropost)
redirect_to user_path (current_user)
end
end
def favouriting
@title = "Favourites"
@user = User.find(params[:id])
@microposts = @user.favouriting.paginate(page: params[:page])
render 'microposts/show_favourite'
end
def favouriter
@title = "Favourite"
@micropost = Micropost.find(params[:id])
@users = @micropost.favouriter.paginate(page: params[:page])
render 'microposts/show_favourite'
end
微观模型:
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :favourites, class_name: "Favourite",foreign_key: "favourited_id", dependent: :destroy
has_many :favouriter, through: :favourites, source: :favouriter
class Micropost
您似乎将用户id作为收藏夹id传递,这对我来说没有意义。这有意义,因为当我喜欢一个微博客时,它喜欢所有用户的微博客,而不是一个。如何使最微小的id成为最受欢迎的id?