byCoder
byCoder

Reputation: 9184

Difficult search in ruby on rails

How can I search first in model (i know how to do this). And then search in this array for more concretence? As you see:

@articles = Article.find(:all, :conditions => { :ART_ID => @search.map(&:ARL_ART_ID)})
@a = @articles.find_all{|item| item.ART_ARTICLE_NR == search.upcase }

First i search in model, but thanks to my db) it have many wrong results, so i must to clarify my array. But there how to search like sql:

like % %

Now it search very strong: if i search AC451, it's good, but if AC45 or C451 it's nothing fetches. How to say him so that before and after

search

could be everything?

Upvotes: 0

Views: 111

Answers (3)

DGM
DGM

Reputation: 26979

You are asking for trouble by not following rails naming conventions an using upper case column names. That said, the rails3 way to do it is probably:

@articles = Article.where(:ART_ID => @search.map(&:ARL_ART_ID)).where('ART_ARTICLE_NR LIKE', "%#{search.upcase}%")

Without knowing what @search is, it's hard to be sure. But you should read up on the active record guide on the rails 3 query format.

Upvotes: 1

Maciej Litwiniuk
Maciej Litwiniuk

Reputation: 419

It's maybe not straigtforward answer, but have you considered using ransack gem?

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230336

Like this, maybe?

 item.ART_ARTICLE_NR.include?(search.upcase)

Upvotes: 1

Related Questions