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.

1
  • 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. Commented Jan 13, 2020 at 6:32

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@SebastianPalma - Oh yeah, in this case there is no need to :select. Thanks! Edited the answer.
I wish my upvotes actually counted so I could show my gratitude, this answer was extremely helpful. Thank you @Prabakaran
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.
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 :)
@SebastianPalma thank you for the tip. I accepted the answer.
0

I think you are looking for this.

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

1 Comment

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.
0

Try the below line :-

<% property_name = ActiveRecord::Base.connection.execute("SELECT name FROM hotels WHERE hotel_id=#{property_id}").first["name"] %>

3 Comments

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
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.
And what's that line supposed to do @AnkurShukla?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.