简体   繁体   English

Rails不等于current_user始终没有结果

[英]Rails does not equal current_user always has no results

I am completely stumped. 我完全陷入了困境。 I'm trying to show objects that do not belong to the current user. 我正在尝试显示不属于当前用户的对象。 Looks like this: 看起来像这样:

activities = Activity.where("user_id != ?", current_user.id)
@activities = activities.near(@user).all( :order => "start_date", :limit => 15)

But when I do this, no results are found, even though my database is full of objects where the user_id is NULL or someone else. 但是,当我这样做时,即使我的数据库中充满了user_id为NULL或其他对象的对象,也没有找到任何结果。 The output looks fine as well to my rookie eyes. 在我的菜鸟眼中,输出看起来也不错。 I can't figure out why nothing is getting pulled. 我不知道为什么什么都没拉。

Output: 输出:

Activity Load (0.7ms)  SELECT activities.*, 3958.755864232 
* 2 * ASIN(SQRT(POWER(SIN((37.7749295 - activities.latitude) * PI() / 180 / 2), 2) + 
COS(37.7749295 * PI() / 180) * COS(activities.latitude * PI() / 180) * 
POWER(SIN((-122.4194155 - activities.longitude) * PI() / 180 / 2), 2))) AS distance, 
CAST(DEGREES(ATAN2( RADIANS(activities.longitude - -122.4194155), 
RADIANS(activities.latitude - 37.7749295))) + 360 AS decimal) % 360 AS bearing FROM 
"activities" WHERE (user_id != 34) AND (activities.latitude BETWEEN 37.4854659337783 AND 
38.0643930662217 AND activities.longitude BETWEEN -122.78562893449197 AND 
-122.05320206550803 AND 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((37.7749295 - 
activities.latitude) * PI() / 180 / 2), 2) + COS(37.7749295 * PI() / 180) * 
COS(activities.latitude * PI() / 180) * POWER(SIN((-122.4194155 - activities.longitude) * 
PI() / 180 / 2), 2))) <= 20) ORDER BY start_date, distance ASC LIMIT 15

If I try the query: 如果我尝试查询:

activities = Activity.where('user_id IS ?', nil)

I get the expected objects. 我得到了预期的对象。 Why do I get no results trying to not equal current_user.id? 为什么我没有尝试不等于current_user.id的结果?

Also, I've tried different permutations like where.not(user_id: current_user.id), <> ?, and a few others. 另外,我尝试了不同的排列方式,例如where.not(user_id:current_user.id),<>?和其他一些排列方式。 And the results are the same. 结果是一样的。

UPDATED 更新

I ended up making user_id's default value "1" rather than NULL. 我最终将user_id的默认值设置为“ 1”,而不是NULL。 Once I did that, the code above worked fine. 一旦这样做,上面的代码就可以正常工作。 I guess my failing is not understanding the properties of nil(?) 我想我的失败不是不了解nil(?)的属性。

Have you tried: 你有没有尝试过:

activities = Activity.where("user_id <> ?", current_user.id)

or (Rails 4) 或(路轨4)

activities = Activity.where.not(user_id: current_user.id)

And (Rails 3) 和(路轨3)

activities = Activity.where(Activity.arel_table[:user_id].not_eq(current_user.id)

... ish ... ish

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM