-1

I am trying to fetch all cards that do not belong to the current user and it's title contains some keywords.

What I have so far after research is this code below.

@other_cards = Card.where(:user_id.ne => @temp_current_user, "title LIKE ?" => "%Business%").limit(10).order('created_at desc down_title asc')

But there are no records being returned even thought they are present.

I have also tried to skip the :user_id condition and all the limits and orders, resulting in this simple code to check if it will return anything

@other_cards = Card.where("title LIKE ?", "%Business%")

enter image description here

EDIT

I have also tried putting the conditions as an array suggested in the first comment. It also resulted in a error

enter image description here

EDIT 2

Attaching screenshot of running the query in rails console

enter image description here

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
A.A. F
  • 349
  • 5
  • 16
  • Please update with your Rails version. `Card.where("title LIKE ?", "%Business%")` should work – razvans Jun 28 '19 at 12:16
  • Please try the same query in rails console and paste its resulting query and output here. – Krupa Suthar Jun 28 '19 at 12:17
  • `@other_cards = Card.where("user_id = ? AND title LIKE ?", @temp_current_user.id, "%Business%").limit(10).order('created_at desc down_title asc')` Please check this query – Vishal Jun 28 '19 at 12:35
  • What rails version do you use? Also do not post errors as images, post as text along with stacktrace (you can take that from development server logs) – Vasfed Jun 28 '19 at 12:35
  • @razvans, it gives the same error, wrong no of arguments – A.A. F Jun 28 '19 at 12:40
  • @Vishal, it also gives the same error. Wrong number of arguments (3 for 1) – A.A. F Jun 28 '19 at 12:40
  • @Vasfed This project is using Rails 4.2.0 – A.A. F Jun 28 '19 at 12:41
  • @KrupaSuthar ArgumentError: wrong number of arguments (2 for 1) Same error as others – A.A. F Jun 28 '19 at 12:43
  • @A.A.F `Card.where("title LIKE ?", "%Business%")` try this. it must work, there is no any rocket science in this simple query. What is type of title ? – Vishal Jun 28 '19 at 12:45
  • @Vishal Yes it is frustrating when code should work but it doesn't like it should :) I have already tried this query as suggested by razvans. Title is a string. – A.A. F Jun 28 '19 at 12:51
  • Can you please add your model here? – Krupa Suthar Jun 28 '19 at 12:53
  • @A.A.F Can you try in console and add screen shot in question? – Vishal Jun 28 '19 at 12:55
  • @KrupaSuthar I am sorry I cannot add the model over here. But title is of type string. the database is implemented in Mongo – A.A. F Jun 28 '19 at 12:57
  • 1
    @A.A.F try this `Card.find({"title": /.*Business.*/})` – Vishal Jun 28 '19 at 12:59
  • 1
    https://stackoverflow.com/questions/8523111/use-like-regex-with-variable-in-mongoid Try this. – Krupa Suthar Jun 28 '19 at 13:01
  • 1
    Querying is different because this is for mongoid (added tag) – Vasfed Jun 28 '19 at 13:08
  • @KrupaSuthar The any.of method works. But how do I add the rest of the conditions I had in the where method? – A.A. F Jun 28 '19 at 13:20
  • @KrupaSuthar Appended the any.of method to the where method at the end. Seems to work fine and fetch records as I wanted – A.A. F Jun 28 '19 at 13:24
  • I was messing around with this today, trying to get a partial search to work with: Rails 7.0.3 Mongoid 7.4.0 Ruby 3.2.1 Many guides suggested to write something like this: Collection.where("title LIKE ?", "%" + params[:q] + "%") I couldn't get it to work and I think it's because MongoDB is a NoSQL type of database. I found where the answer seems to work: https://stackoverflow.com/questions/53949266/how-can-i-write-like-statement-in-rails-for-mongodb I ended up writing like this: Collection.where(name: /.*(#{Regexp.escape(query)}).*/i) – Johan Samuelsson Nov 21 '22 at 15:10

1 Answers1

1

LIKE syntax is part of SQL. MongoDB, and Mongoid, do not (directly) support SQL. The general rule of thumb is Ruby methods carry over from ActiveRecord to Mongoid, for example the following works with both AR and Mongoid:

Card.where(id: 123)

... but once you start to specify complex conditions in either keys or values, they are often specific to the database you are working with. In the code you provided, the :user_id.ne syntax will not be recognized by ActiveRecord, and Mongoid/MongoDB do not recognize title LIKE ?.

Your problem statement was:

I am trying to fetch all cards that do not belong to the current user and its title contains some keywords.

MongoDB has native full text search capability, which is a superior solution to trying to match keywords with regular expressions or SQL LIKE operators. MongoDB has good documentation on full text searches here. The Ruby driver has further documentation, and examples, for how to use full text search here. Mongoid does not currently have documentation for full text search (there is an in progress pull request to add it here) but the gist of it is to combine the driver documentation referenced earlier with the documentation here for how to work with indexes in Mongoid in general.

D. SM
  • 13,584
  • 3
  • 12
  • 21