Ruby on Rails SQL查询返回#

时间:2022-10-08 09:49:54

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


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)