简体   繁体   中英

Why isn't my search method working in Ruby on Rails?

In my Ruby on Rails application, I have a cinema system and am trying to return the screen a showing is in when a user searches for the showing.

To display the search drop down I am using this code in my _application.html.erb:

<%= render( :partial => '/screen_lookup', :locals => {:showings => @showings = Showing.all, :my_path => '/screens/display_screens_by_showing' })%>

Which renders the search from the _screen_lookup.html.erb:

<%= form_tag my_path, :method=>'post', :multipart => true do %>

    <%= select_tag ('showings_id'), 
        options_from_collection_for_select(@showings, :id, :showing_times, 0 ),
        :prompt => "Showings" %> 

    <%= submit_tag 'Search' %>
<% end %>

And uses the display_screens_by_showing in the screens_controller:

  def display_screens_by_showing
    @screens = Screen.showing_search(params[:showing_id])
    if @screens.empty?
        # assign a warning message to the flash hash to be displayed in
        # the div "feedback-top"
        flash.now[:alert] = "There are no films of that genre."
        # return all products, in alphabetical order
        @screens = Screen.all
    end
    render :action => "index"
 end

And this searches using the method in the screen.rb model:

def self.showing_search(showing_id)
    screen = Showing.where("id = ?", showing_id).screen_id
    self.where("id = ?", screen)
end

Now, the problem I am having is that because a showing belongs_to a screen, and a screen has_many showings, I need to be able to search for the showing, and store that showing's screen_id in a variable to search for the screen that showing is in with, which I have tried doing in the model:

screen = Showing.where("id = ?", showing_id).screen_id
self.where("id = ?", screen)

But the error I am getting is:

NoMethodError in ScreensController#display_screens_by_showing
undefined method `screen_id' for #<ActiveRecord::Relation []>

These are the model relationships:

showing.rb:

class Showing < ActiveRecord::Base
    belongs_to :screen
end

screen.rb:

class Screen < ActiveRecord::Base
    has_many :showings
end

What code will get my search working?

The problem is that where doesn't return a record, it returns a relation that can be enumerated or chained, instead you want to use find or find_by to return a single record, which is kind equivalent to where + first

screen = Showing.find(showing_id).screen_id

which is sort of like doing

screen = Showing.where(id: showing_id).first.screen_id

If you want to pass a hash you can use find_by which will be like this

screen = Showing.find_by(id: showing_id).screen_id

PS:
I'm not sure what you're doing exactly, but i think those two lines can be merged into a single query ( not sure what it should be returning, but I'm assuming a screen )

def self.showing_search(showing_id)
    Showing.find(showing_id).screen
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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