Difficult search in ruby on rails - 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?

Like this, maybe?
item.ART_ARTICLE_NR.include?(search.upcase)

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.

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

Related

Rails Modify Array in Loop

I thought this would be a simple task, but I'm finding it difficult to make Rails do what I want.
I've got an array of dates.
So I thought that something like this would work:
def index
#datetimes = Books.all.map(&:checkouts).flatten.map(&:out_date)
#datetimes.each do |c|
c.to_date
end
end
Then I can just call this in my view:
%ul
-#datetimes.each do |c|
%li=c
How do I modify each key in the array? What am I missing here?
Thanks, so much for being nice to new, novice, and ignorant hobbyists like myself.
.each doesn't modify the caller. It simply loops through. You could change the controller action to just this:
#datetimes = Books.all.map(&:checkouts).flatten.map{|e| e.out_date.to_date}
You might also want to explore including :checkouts in your Books query to avoid N+1 queries. Or perhaps doing something like this maybe.
Checkout.where("book_id is not null").map{|e| e.out_date.to_date}

Sorting data from database with Rails

I suspect my question is stupid and answer is obvious, but I TOTALLY stuck with it.
In controller movies_controller.rb I have method index, which fills the array #movies with values from database by calling #movies = Movie.all, and method sort, which do almost the same, but use #movies = Movie.order(:title) instead.
If I call sort explicitly from index what I see in View Index.html.haml is sorted by title list of movies as I expect. But, how can I call method sort by clicking on link? I try
In Index.html.haml:
%th= link_to 'Movies title', 'movies', :on_click => 'self.sort'
In routes.rb:
match ":controller => movies, :action => sort" => 'movies/index'
and nothing is happening, method sort never executes. Actually, I have no idea how to do it right. Looks like it is obvious for everyone, but me.
Can I find short answer how to do it right? Please, kick me in right direction!
If you are looking for sorting data in table then you need some plug in to in place sort the table according to some attribute. following jquery plugin help you to do that,
https://github.com/linjunpop/jquery-tablesorter-rails
or you can achieve the same with background sorting with order clause. check out the following,
http://railscasts.com/episodes/228-sortable-table-columns
For quick and easy you can use 'ransack' gem.
For some writing by yourself check this out http://railscasts.com/episodes/228-sortable-table-columns

Ruby set dictionary for search with activerecord

In my rails app i'm fetching data from mysql database, part of code:
#search = ArtLookup.find(:all, :conditions => ['MATCH (ARL_SEARCH_NUMBER) AGAINST(? IN BOOLEAN MODE) and ARL_KIND = 1', search_condition.gsub(/[^0-9A-Za-z]/, '')])
But main trouble that i have different suppliers price list's, and there i have different coding's for same variable in db, for example:
LEMFÖRDER
But how can i set dictionary for my search_condition so that if my search_condition is for example:
LEM?FORDER or
LEMFOERDER or
LEMFÖRDER
It will find my LEMFÖRDER in db?
I know that it could sound very strange, sorry for my english, but i explain all on my example...
I think that, in this case, you should start using a library to deal with full-text-search and additional search capabilities, like Solr or Sphinx.
Take a look at http://pat.github.com/ts/en/searching.html.
This kind of complexity is common and it is already implemented in many algorithms.
Hope it helps!
You could do this by using ActiveRecord's AREL engine like the following:
def lookup(*alternatives)
match_condition = 'MATCH (ARL_SEARCH_NUMBER) AGAINST(? IN BOOLEAN MODE)'
or_conditions = alternatives.map do |alternative|
ArtLookup.where(match_condition, alternative).
where_values.reduce(:and)
end
and_condition = ArtLookup.where('ARL_KIND = 1').where_values.reduce(:and)
# Build a disjunction
conditions = or_conditions.shift
or_conditions.each do |condition|
conditions = conditions.or(condition)
end
# Build the final conjunction
conditions = conditions.and(and_condition)
ArtLookup.where(conditions)
end
Then you can find the objects like the following:
#search = lookup('LEM?FORDER', 'LEMFOERDER', 'LEMFÖRDER')
Or directly provide an array:
alternatives = [
'LEM?FORDER',
'LEMFOERDER',
'LEMFÖRDER'
]
#search = lookup(*alternatives)
I'm aware of the fact that this is far too much code for the simple thing it's doing. But it should do it and I'm not aware of a much better way. I didn't test that code, so it could contain some minor mistakes.
If I've understood your question correctly, you want to have Mysql treat those three values as the same thing. Now, assuming that they are considered the same thing in a specific language (for example, ß = ss in German), Mysql will handle this automatically based on your collation settings, so selecting the correct collation should fix it for you.

How do I re-write "Feedback.where('poster_id = 3 OR receiver_id = 3')" in Rails 3.1?

Is there a way to write it in a more 'Rails-friendly' way?
i.e. if I were searching for anyone of those attributes, I would just do Feedback.where(:poster_id => 3) and it would be good.
But how do I achieve the OR in the Rails 3 friendly syntax (as opposed to the SQL friendly one).
Also, if it is better to use my original one over the desired one, why is it better?
Thanks.
Edit 1: Btw, if I do Feedback.where(:poster_id => 3, :receiver_id => 3) that returns the result for the AND operation, which is the exact opposite of what I want. So I feel so close, just not quite there.
You can do this by putting SQL fragments in the where() arguments. For more information you can look at the ActiveRecord querying guide.
Feedback.where("poster_id = ? OR receiver_id = ?", 1, 3)
You can do this without SQL fragments as described in this SO post:
t = Feedback.arel_table
Feedback.where(t[:poster_id].eq(1).or(t[:receiver_id].eq(2)))

Like and where condition in ruby

What is the syntax for like in Ruby on Rails? This is something I'm trying to do:
I am trying to find all the last name from table which starts with egm so something like %egm%. I know how to do using find_by_sql but just curious to know the Ruby way.
s = Person.find_by_last_name('nan%')
Person.where('name LIKE ?', '%egm%').all
l_name_var = "nan"
Person.where("people.last_name LIKE :l_name", {:l_name => "#{l_name_var}%"})
or in your case
l_name_var = "egm"
Person.where("people.last_name LIKE :l_name", {:l_name => "%#{l_name_var}%"})
To expand a bit, the find_by_X methods use the = operator, so you wouldn't want to use them for a like condition. The "Rails" way involves using a bit of SQL inside of the where method as shown in the other answers. The same would apply if you're trying to sort your results using the order method.

Resources