【发布时间】:2015-10-06 19:29:44
【问题描述】:
我正在尝试通过连接表获取记录数组。我想找到一个用户的所有收藏夹,这些收藏夹都是蓝色的,但都是“当前的”。 User_Favorites 可以过期。这就是我正在尝试的:
用户.rb
has_many :user_favorites, dependent: :destroy
has_many :favorites, through: :user_favorites
Favorite.rb
has_many :user_favorites, dependent: :destroy
has_many :users, through: :user_favorites
UserFavorite.rb
belongs_to :user
belongs_to :favorite
scope :current_as_of, -> (date) do
where('start_date <= ?',date).
where('(end_date >= ? or end_date IS NULL)', date)
end
scope :blue, -> { where('self.favorite.color = ?','blue') }
类用户控制器 这是我得到的错误:def profile
@user = User.find(params[:id])
@blue_favorites = @user.user_favorites.current_as_of(Date.today).blue.all
end
There is an Error: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "favorite"
LINE 1: ...d_date >= '2015-10-06' or end_date IS NULL)) AND (Favorite.co...
^
: SELECT "user_favorites".* FROM "user_favorites" WHERE "user_favorites"."user_id" = $1 AND (start_date <= '2015-10-06') AND ((end_date >= '2015-10-06' or end_date IS NULL)) AND (Favorite.color = 'blue')
【问题讨论】:
标签: ruby-on-rails postgresql ruby-on-rails-4 jointable