0

i would like to get a arrau of boats, but i would like that just show the boats with rent boolean set to true.

how can acomplish this?

i've added:

@boats = Boat.where(rent: true)

But did not worked

i even tried the method model

def rents
    self.class.where(rent: true)
  end

and did not worked either

4
  • 1
    Do you want an array of the names? or you you want a hash of objects? @boats = Boat.where(rent: true) should've worked if you have your tables setup properly for a set of objects. Commented Jun 9, 2021 at 16:24
  • 1
    What do you mean by did not worked? Was there an error? What was the error message and how did the stack trace look like? Or did you receive an unexpected result? How did it look like? Commented Jun 9, 2021 at 16:26
  • Your code seems OK, do you get an error when you try to execute the code? that might help. Commented Jun 9, 2021 at 16:29
  • @boats = Boat.where(rent: true) should work. Make sure that you actually have boats with rent = true Commented Jun 9, 2021 at 18:37

1 Answer 1

1

You can do something like this

<% @boats.where(:rent=>true).each do |t| %> 
 <%= t.name %>
<%end%>
<%= @boats.where(:rent=>true).all %>

or

<% if boats.where(:rent=>true).exists?  %>
 <%= t.name %>
<%end%>

or

def rents
 @boats.where(:rent=>true).all
end
Sign up to request clarification or add additional context in comments.

1 Comment

nourza! the array on view works fine but not on the controller! thank you very much

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.