I built a small twitter clone in Ruby on Rails. It has a User model, a Micropost model, and a Relationships model. The Relationships model stores followed user ids and corresponding following user ids. I am trying to add a new button that makes the current user follow all other users with a matching parameter in their microposts. I have added that parameter to the micropost model. The problem is, when I query the database in the micropost model to find users with that matching parameter it returns

我在Ruby on Rails中构建了一个小的twitter克隆。它具有User模型,Micropost模型和Relationships模型。 Relationships模型存储了用户ID和相应的后续用户ID。我正在尝试添加一个新按钮,使当前用户在其微博中跟随所有其他用户使用匹配的参数。我已将该参数添加到微博模型中。问题是,当我在微博模型中查询数据库以查找具有该匹配参数的用户时,它返回

#<ActiveRecord::Relation:...

Why is it doing this?

它为什么这样做?

Here is my form code that includes the button:

这是我的表单代码,包括按钮:

<%= form_for(current_user.relationships.build(:followed_id => current_user.matched_user), :remote => true) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Found A Matching User - Click Here To Follow Them" %></div>
<% end %>

Here is the referenced matched_user definition in the user model:

以下是用户模型中引用的matched_user定义:

def matched_user
Micropost.matched_with(self)
end

Here is the referenced matched_with method in the micropost model. I tried a few different things so I noted each error I am getting for each set of lines.

这是微博模型中引用的matched_with方法。我尝试了一些不同的东西,所以我注意到每一行都会得到的每个错误。

def self.matched_with(user)         

# matched_microposts = Micropost.find_by_parameter(:parameter)
# where("user_id IN (#{matched_microposts}) OR user_id = :user_id", :user_id => user)

# ERROR: ActiveRecord::RecordNotFound in RelationshipsController#create
# Couldn't find User with id=#<ActiveRecord::Relation:0xb59c71dc>





# matched_id = Micropost.where("parameter = ?", :parameter)
# where("user_id IN (#{matched_id}) OR user_id = :user_id", :user_id => user)

# ERROR: ActiveRecord::StatementInvalid in Pages#home
# SQLite3::SQLException: unrecognized token: "#": SELECT COUNT(*) FROM "microposts"
# WHERE (user_id IN (#<ActiveRecord::Relation:0xb5b7d3b4>) OR user_id = 101)





# matched_ids = Micropost.find_by_sql "SELECT user_id FROM microposts WHERE parameter = :parameter"
# where("user_id IN (#{matched_ids})", :user_id => user)

# ERROR: ActiveRecord::RecordNotFound in RelationshipsController#create
# Couldn't find User with id=#<ActiveRecord::Relation:0xb5a38850>

end

Here is my relationships controller create method:

这是我的关系控制器创建方法:

def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end

And finally, here is my user model follow! method:

最后,这是我的用户模型关注!方法:

def follow!(followed)
relationships.create!(:followed_id => followed.id)
end

Thank you very much any help you can provide. It is very much appreciated.

非常感谢您提供的任何帮助。这是非常赞赏。

1 个解决方案

#1


1

first of all Model.where returns a relation object to which you can chain other operators(another .where or .order, etc)

首先,Model.where返回一个关系对象,你可以链接其他运算符(另一个.where或.order等)

if you want a record to be found in db you have to explicitly close that chain with .first, .last, .all or even iterate over results using .each

如果你想在db中找到一条记录,你必须使用.first,.last,.all显式关闭该链,甚至使用.each迭代结果

second - why are you trying to directly insert results into string using #{matched_ids}? that will call #to_s on a relation object instead of properly building list of these objects to be used in a query

第二 - 为什么要尝试使用#{matched_ids}直接将结果插入字符串?这将在关系对象上调用#to_s,而不是正确构建要在查询中使用的这些对象的列表

try this:

尝试这个:

where("user_id IN (?)", matched_ids)

更多相关文章

  1. 保存在Java桌面应用程序应用程序和网站上使用的个人用户设置的最
  2. 装机建项目vs2017和mysql5.7下建项目用EF建实体模型的过程..
  3. 是一个mysql临时表,每个用户访问创建它的脚本是唯一的...?
  4. MVC框架——学生信息管理系统(多表,多事务如何处理,一个用户如何共
  5. MySQL5.7以上版本root用户空密码修改(windows系统、zip版MySQL)
  6. MySql查询脚本,每月统计活动用户。
  7. MySQL用户管理(5.7.20-winx64)
  8. Laravel - 如何为用户赋予多重角色?
  9. Linux下修改MySQL用户(root)密码

随机推荐

  1. GridView
  2. 设置透明,模糊,黑暗度
  3. android aosp 下载源代码
  4. android中关于margin的一些注意点
  5. (20120722)(笔记002)android开发环境搭建
  6. Android资料备注
  7. android:padding 与 android:margin的区
  8. [图解]Android下Invalid layout of java.
  9. Android 软键盘遮挡Dialog
  10. 错误:AGPBI: {“kind“:“error“,“text