0

This is the current code for my query..

<% property_id = params[:property] %>
<%= property_name = Hotel.find_by_sql('
        SELECT name 
        FROM hotels
    ') %>

I want to be able to add something like

WHERE hotel_id == property_id

But everything I try doesn't seem to work due to the "property_id" portion. I've tried concatenating, different assignments, etc. I feel dumb. SOS. Thank you ahead of time.

Also, when I add..

WHERE hotel_id == "hotelid1"

Which "hotelid1" is an existing hotel_id in the table. It works but not how I would imagine. It returns..

"[#Hotel id: nil, name: "HotelOne">]"

I'm wanting it to only output the hotel name. In this case, HotelOne.

Haha
  • 278
  • 2
  • 10
  • 1
    Are you using Rails? Are you using Active Record? Why not just `Hotel.find_by(hotel_id: hotel_id).name` if you have the `hotel_id` available somewhere? Also, better in your case to move that query from your view. – Sebastián Palma Jan 13 '20 at 06:32

3 Answers3

1

ActiveRecord's where should be suffice for you.

property_names = Hotel.where(hotel_id: params[:property]).pluck(:name)

And it's confusing to have hotel_id in Hotel model as it contradicts with the default id attribute. Anyhow hope this was useful to you.

Note: If hotel_id is unique in your table then better to go with @SebastianPalma's comment in your question.

Prabakaran
  • 355
  • 2
  • 10
  • 1
    @SebastianPalma - Oh yeah, in this case there is no need to :select. Thanks! Edited the answer. – Prabakaran Jan 13 '20 at 09:46
  • I wish my upvotes actually counted so I could show my gratitude, this answer was extremely helpful. Thank you @Prabakaran – Haha Jan 13 '20 at 13:20
  • 1
    Actually, due to your points, your upvotes are just going to be saved as a draft until you get the needed ones to persist them. If this answer helped you to solve your problems, you should accept it. This shows future readers that this answer solves the problem stated in the question. – Sebastián Palma Jan 13 '20 at 16:33
  • Actually @SebastianPalma your comment is THE answer and I would suggest posting it as the answer but as OP got help from this answer too so it's his preference to accept any answer :) – ARK Jan 13 '20 at 17:59
  • @SebastianPalma thank you for the tip. I accepted the answer. – Haha Mar 04 '20 at 03:02
0

I think you are looking for this.

<%= property_name = Hotel.find_by_sql("SELECT name FROM hotels WHERE hotel_id=#{ruby_variable}") %>

  • I upvoted to cancel the downvote and you get 8 extra reputation. There you go. And yeah I know it works but @Sebastian Palma's answer is rails-oriented and better. – ARK Jan 13 '20 at 17:55
0

Try the below line :-

<% property_name = ActiveRecord::Base.connection.execute("SELECT name FROM hotels WHERE hotel_id=#{property_id}").first["name"] %>
Ankur Shukla
  • 345
  • 2
  • 9
  • 1
    Whoever has downvoted this answer, for his information, this code gives exact answer as expected, as required and asked in the question. The person downvoting it is has no sense of RoR – Ankur Shukla Jan 13 '20 at 11:38
  • I upvoted to cancel the downvote and you get 8 extra reputation. There you go. And yeah I know it works but @Sebastian Palma's answer is rails-oriented and better. – ARK Jan 13 '20 at 17:54
  • And what's that line supposed to do @AnkurShukla? – Sebastián Palma Jan 14 '20 at 06:35