Home>

[Premise]
I'm writing an application in Rails.
I am in trouble, so I would appreciate it if you could tell me.
I am writing a code to get the product associated with the user using the search function.

[What I want to achieve]
Implementation of search function
I want you to search by user name from the product list and display the products of the corresponding user.

The flow of data acquisition in binding.pry is also attached.
There are many poor points, but we appreciate your cooperation.

ActiveRecord :: RecordNotFound in UsersController # search
Couldn't find Item with'id'= {: user_id =>[1]}
The item with user_id = 1 exists in the DB.
Corresponding source code
(model/user)
(Column name, etc. omitted)
   def self.search (search)
      if search! = ""
        User.where ('nickname LIKE (?)', "% # {Search}%")
      else else
        User.all
      end
    end
  #Association
    has_many: items
(model/item)
  #Association
   belongs_to: user

(controller/users)
  def search
    binding.pry
    @user = User.search (params [: nickname])
    @users = @ user.ids
    @items = Item.find (user_id: @users) =>Error target
    # @item = Item.all =>items table data can be obtained
  end

(search.html.erb)
<% = form_with (url: search_users_path, local: true, method:: get, class: "search-form") do | f |%>
<% = f.text_field: nickname, placeholder: "Search for posts", class: "search-input", size: "50"%>
<% = f.submit "search", class: "search-btn"%>
<% end%>
(Omitted)
<% @ items.each do | item |%>
(Omitted)
Eventually, I'm trying to display @items one by one for that user's products.
What I tried

Confirmation of data acquisition using binding.pry.

The result of binding.pry.
[1] pry (#)>@user = User.search (params [: nickname])
User Load (0.6ms) SELECTusers. * FROMusersWHERE (nickname LIKE ('% mountain%'))
↳ app/controllers/users_controller.rb: 4: in `search'
=>[#]
[2] pry (#)>@users = @ user.ids
=>[1]

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

ruby '2.6.5'

  • Answer # 1

    find is a method to find the ID.
    {: user_id =>[1]}I have searched for the ID (error because there is no)

    @user = User.search (params [: nickname])
    Item.where (user_id: @ user.ids)