Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Nano Meko
Nano Meko
1,838 Points

Filter results in ROR application (treebook)

What is the general direction to take if I want to filter my statuses. For example if I only want to display the statuses created between certain dates, or by a certain user.

Is there a query language I have to use or does Ruby on Rails provide something?

4 Answers

James White
James White
6,159 Points

You can use SQL. I'm running low on time right now so this won't be detailed.

In your controllers folder you can create a method, call it "search" that can be written to search through the database.

There's 3 rails methods: find_by_field: Find the first record in the specified data field that matches a value. find_all_by_field: Find all records in the specified data field that match a value. find_by_sql: Find records using a customized SQL statement.

I've only done this once through an online class so I'd have to play around with the code for treebook, but the search method will look something like:

def search
    pattern = params[:searchFor]
    pattern = "%" + pattern + "%"
    @reviews = Review.where("title like ?", pattern)
end

You'll have to change title in the @reviews line.

Then in your search form you'll need to set the for_tag

<%= form_tag :action => 'search' do %>
<label><font color="#663300" size="-1">Search for review:</font> </label>
<%= text_field_tag :searchFor %>
<%= submit_tag "Find" %>
<% end %>

This code will need to be changed for treebook. And there might be some pieces I'm missing since I'm in a hurry.

I'll try to follow up later tonight.

Nano Meko
Nano Meko
1,838 Points

Thanks a lot for this!

Hey man, did you end up figuring it out? Having a lot of trouble with this

Hey man, did you end up figuring it out? Having a lot of trouble with this

This might help as well: Active Record Query Interface

Nano Meko
Nano Meko
1,838 Points

Thanks Philipp that indeed in helpful