Topic: How do we build 'End User' queries?

My fundamental stumbling block now is how to allow 'user searches' and maintain a RESTful design?
When I create an INDEX action it always looks something like this;
@tablename = Tablename.find(:all)
To maintain a RESTful design, I surmised that I would perform a SEARCH using methods that I've built into my tablename.rb model file and call them in my INDEX action?
Here I'm stumbling...would I have logic like this;
def index
  unless called by search
    @tablename = Tablename.find(:all)
  else
    @tablename = search.result.collection
end
How would I access if the user had come in for the first time or from a SEARCH form?
I've bought every book I can find in Rails and not one shows how to do this? Obviously, the result set of this will drive my eventual reports.
Thank you,
David
P.S. Please no acts_as_ferret suggestions as this is WAY off point.

Re: How do we build 'End User' queries?

something like this:

# controller
def index
  if params[:search].nil?
    @items = Item.search(params[:search])
  else
    @items = Item.find(:all)
  end
end

# view (in a form)
<%= text_field_tag :search, params[:search] %>

# Item model
def self.search(keywords)
  find(:all, ...)
end

Railscasts - Free Ruby on Rails Screencasts

Re: How do we build 'End User' queries?

Ryan,
How did you get the values into the :search field? Here is the part that eludes me:
# view (in a form)<%= text_field_tag :search, params[:search] %>
From what I've studied...:search is the name of the text field the user is placing the data into. Where do those params[:search] come from? What are they supposed to be?
What does that self.search()method really look like? What are the parameters that are passed after the :all?
I know you know this stuff, but I can't see the missing holes.
Thanks,
David

Re: How do we build 'End User' queries?

BraveDave wrote:

From what I've studied...:search is the name of the text field the user is placing the data into. Where do those params[:search] come from? What are they supposed to be?

When the form is submitted (the user presses the search button) the params[:search] gets filled with the value the user typed in the search field.

BraveDave wrote:

What does that self.search()method really look like? What are the parameters that are passed after the :all?

Depends what attributes the model has and how you want the search to behave.

Railscasts - Free Ruby on Rails Screencasts

Re: How do we build 'End User' queries?

Ryan,
Are you saying the user made on SINGLE entry to ONLY search on ONE field?
I think I'm going to have to allow users' to search on multiple fields.
Do you see what I'm asking? Can you help me see how one would search on multiple fields.
David

Re: How do we build 'End User' queries?

Well, you can have the user's single field input search over multiple fields (this is what you specify in the "find" method). If you have two fields in a model, let's say first_name and last_name, and the user types "John" in the text search field, you can have it search both first_name and last_name fields for that string.

Or you can have multiple text fields, one for first name and one for last name, it's up to you. I can't tell you what to do here because it depends on the requirements of your app.

Railscasts - Free Ruby on Rails Screencasts

Re: How do we build 'End User' queries?

This is getting ridiculous. Do you know anyone that I might approach to ask these questions as this keeps going in circles.

Re: How do we build 'End User' queries?

You are discovering the hard way that search is not a "just add water" affair. I was disappointed too..Rails makes everything else so easy that I resented the challenge at first. But it is rewarding, after weeks of banging my head against the wall, that my code works as expected, and I have another tool in my kit for future projects.

As for your problems, if you want more solid answers then you should be more clear with your questions. Post code examples and tackle one issue at a time, or you will quickly end up overwhelmed and frustrated.

Re: How do we build 'End User' queries?

I think I'm going to take the tack of leaving Rails out of the affair.
I can certainly build a form where the user selects tables and fields to query upon. Once this string is 'digested' by MySQL then I can hook Rails back up to the result set.
What fascinates me about this whole departure from classic Rails is that it exposes the fact that there are few, if any, business Rails applications out there. I suppose Web 3.0 is all talk.
Thank you for the kind advice. You are a valuable ally, Mr. Pimpmaster.

Re: How do we build 'End User' queries?

No Prob BD... Your solution sounds totally viable. If you are comfy with MySQL like that, I say go for it. Personally I was using My SQL fulltext on a past project and found it limiting in terms of how you can set up relevance priority with certain fields. It may be possible, but I am much more at home with Ruby than SQL, if you can imagine that...

As for business Rails apps, there are quite a few out there, but I find that the majority of projects are just coders having fun with very personalized apps. In the cases of enterprise solutions, the only folks that would usually code a search engine from scratch are usually gurus who code Ruby in their sleep. The rest of us mortals tend to rock plugins because

1. The code is tested
2. Even if the docs suck, we can at least bother the authors for answers. wink

Good luck with your search!

Oh and BTW, to make your posts easier to digest, try slapping your code examples inside BBcode tags. Like this

{code=ruby}def this_is_cool{/code}

Obviously you must substitute those for square brackets so it will print like this

def this_is_cool

Re: How do we build 'End User' queries?

Pimpmaster, I am obviously exposing my naivety here, but I'm new to MySQL AND Rails and have spent so many months pouring through Rails books, that I'm at a point of no return.
Firstly, are you saying that if I write this "{code=ruby}def this_is_cool{/code}" it will show up looking like that 'cool blue' code you guys do? I've always been amazed why there's no help tools on these forums?
Anyway, do you know any of these Ruby 'gurus' that I might approach to hire or buy such an example?
Thanks,
David
775-885-9125

Re: How do we build 'End User' queries?

BraveDave wrote:

Firstly, are you saying that if I write this "{code=ruby}def this_is_cool{/code}" it will show up looking like that 'cool blue' code you guys do?

Yup, just make sure you use the square brackets [] instead. I used curly braces just to show u the structure.

BraveDave wrote:

Anyway, do you know any of these Ruby 'gurus' that I might approach to hire or buy such an example?

My deductions come from months of intense research via blogs and wikis. I did email a few folks about this ages ago.. if I remember correctly, the only one that contacted me back was Chad Fowler..you may recognize him from all your readings. I doubt he is available for hire, but it never hurts to ask.

chad [at] chadfowler [dot] com