MongoID Like query on embedded model - ruby-on-rails

I have User model embeds_one :profile and Profile model has name. I need to run LIKE query on profile name. I tried below as suggested here
User.where("profile.name" => "/.*Senthil.*/")
But above solution not working. I tried lot of stock overflow answers , but no luck. Any help will be highly appreciated.
Screenshot : I am very sure , there is matching record.

This finds all people with the name senthil (first OR last).
User.where("profile.name" => /.*senthil.*/i )
Here i used to make the query case insensitive.

I think you mean to remove the quotes, otherwise the engine will try to match the string exactly, instead of as regex
edit: The correct regexp would be
User.where("profile.name" => /.*Senthil.*/)

Related

If condition on RABL

I have two attributes in my relation - flyer and flyer_url. I want to put an if condition which assigns either flyer or flyer_url to flyer_image_url depending on which is not null. All the records have either of them set to null. I've tried the following code but it is only assigning flyer_url to flyer_image_url even when it is null:
attribute :flyer => :flyer_image_url
attribute :flyer_url => :flyer_image_url, :if => lambda { |flyer_url| !flyer_url.nil? }
Please help!
Thanks!!
Thanks to Nathan Esquenazi - the creator of RABL, I finally got the answer.
This is a limitation (although somewhat intentional) of RABL, for a given key name “flyer_image_url” there should only ever be a single statement associated with it. Trying to have two statements associated with a single key is what is causing the confusion here.
He suggests using a single node instead with custom logic: https://github.com/nesquena/rabl#custom-nodes
node :flyer_image_url do |object|
object.flyer_url.nil? ? object.flyer : object.flyer_url
# This is just ruby, put whatever you want that ends up as a string
end
That worked like a charm! Hope this answer finds somebody!

Boost exact match searchkick / elastic search Rails 4

Having trouble getting exact matches to display first. I am using searchkick with elastic search on my rails 4 app.
For example, if a user searches "coke" .. "coke zero" will display first. I would like it the other way around.
If there is documentation on this can you please point me in that way? I am having an overly hard time finding a solution.
I have tried boosting the title field ('specific' in my case):
fields: ["specific^20"]
and boosting where the field matches the query exactly (although I don't know if i'm implementing this correctly):
boost_where: [:specific == :q]
Nothing seems to be working. Thank you!
Try this .....
Model.search "hi#example.com", fields: [{column_name1: :exact}, :column_name2]
Hope this will help you.
I had a similar issue. I solved it like this.
Model.search(
'search term',
fields: [
{'name^2' => :phrase},
{'name' => :word_start},
]
)

Rails ActiveRecord update_all array of ids

I have an ActiveRecord Model called Animal.
Animal has id and client_id.
In my app have an array called #selectedanimals that contains the id's of the animals I want to update such as: #selectedanimals: ["6", "14", "5"].
I have the value of a new client_id for these animals like #newclient.id and I want to update all of these Animal records with the new client_id.
What I have now is:
Animal.update_all({:client_id => #transferclient.id}, {:id => #selecteanimals})
I know this is not 100% correct because it is having a problem with :id.
I get an error like this:
Called id for nil, which would mistakenly be 8 -- if you really wanted the id of nil, use object_id
Forgive my ignorance but this is my first time using update_all and I don't see any examples where you pass it an array of the ids of the records you want to update so any help would be appreciated much.
EDIT:
Apparently the #transferclient.id was not properly defined. That was my problem.
Thanks all.
I am new to Ruby so I don't know if there's been some recent change in the API but Danpe's answer as well as the initial suggestion that actually worked according to the asker, didn't work for me. They both raised a syntax error.
The solution that worked for me and is actually proposed in the API documentation is this:
Animal.where(:id => #selectedanimals).update_all(:client_id => #transferclient.id)
You need to check if id is inside that array:
Animal.update_all({:client_id => #transferclient.id}, {"id IN (?)" , #selecteanimals})

Regex to split email address between two characters # and .

I am trying to use Regex to get the company name out of the email address. I am splitting the email two times. Is there a better solution for this?
c = "user#company_name.com"
(c.split("#").last).split(".").first
Another solution given.
str = "user#company_name.com"[/[^#]+(?=\.)/]
See working demo
Answer to the question in the post (before it was edited)
Judging from your code, it seems like you want to extract the top level domain (although it contradicts with the title, which does not make sense). Assuming so, this will give you the top level domain.
"user#company_name.com"[/[^.]+\z/]
# => "com"
Solution to a different problem that the OP additionally mentions in the comment to this answer
"user#company_name.com"[/(?<=#)[^.]+/]
# => "company_name"
This will give you company name.
(.*)#(\w+)\.(.*)

Unable to search by name and primary key

The following query doesn't search by id (primary key) at all - it keeps assigning 0 to id and search_str to name. It does so properly if we use equals operator instead of matches for "id". Is there an issue with the following scope.
scope :or_search, lambda {|name| where(arel_table[:name].matches("%{search_str}%").or(arel_table[:id].matches("%#{search_str}%")))
I'm a little confused by what you're trying to do but first
arel_table[:name].matches("%{search_str}%")
You're missing the #, I think this should be
arel_table[:name].matches("%#{search_str}%")
Second you're not using your name argument anywhere, just from looking at this, maybe you want to replace search_str with name?
first of all you have made a typo which i think is you missed one closing curly brace at the end of the scope defination
Coming to an answer i think you are doing something which is either match id or title?(really i can not believe this you should not do this)
Note the "search_str" is coming from nowhere because the parameter to the block is "name" and hence you can compare it with whatever attribute of the table you want to compare :)
And the answer i can think for your problem is
scope :or_search, lambda {|title| where(arel_table[:title].matches("%#{title}%").or(arel_table[:id].matches("#{title}")))}
pare
Not sure if you're just trying to understand why it doesn't work, or if you're looking for a working solution.
Anyway the following isn't in arel, but would work if you add it to the model:
def self.or_search(str)
where("name like :input OR id like :input", input: "%#{str}%")
end

Resources