Posts

Showing posts with the label activerecord

How to check if a number is within range in activerecord?

Image
Clash Royale CLAN TAG #URR8PPP How to check if a number is within range in activerecord? I have a model with a range column #<JobRequirement id: 1, age: 18...30> How can I get the JobRequirement using an age: 20 ? JobRequirement age: 20 Something like JobRequirement.where(age: 20) Which database and what type is age inside the database? – mu is too short 11 hours ago age It is int4range in postgreSQL – Brian Law 11 hours ago 1 Answer 1 I think you need to use the PostgreSQL range operators and a bit of SQL in a string. In particular, you'd want the @> (contains element) operator: @> JobRequ...

Removing spaces before querying database in Active Record works in Rails C but not controller

Image
Clash Royale CLAN TAG #URR8PPP Removing spaces before querying database in Active Record works in Rails C but not controller I am setting up an AJAX response to auto-populate the data list on a search bar as the user types. So far it works perfectly, except that if the user adds additional spaces in the word, it won't find the result. I am trying to remove the spaces when querying the database for similar results by adding some additional SQL into my .where arguments. It still works for the previous case of when there are no extra spaces, but when the user enters extra spaces it does not work. The Listing.where("replace(city,' ','') like replace(?, ' ', '')", query) query works perfectly when I simply run it in Rails console for testing, so I don't understand why it's not working from the controller method. Listing.where("replace(city,' ','') like replace(?, ' ', '')", query) Here is my c...

Double nested association w/ Cocoon and Polimorphism gets rejected

Image
Clash Royale CLAN TAG #URR8PPP Double nested association w/ Cocoon and Polimorphism gets rejected So, I have a model Label which is polymorphic and another model Stuff which is not. A stuff can have many labels (may also called groups), and each and every one of them can also have a label . I am working with the Cocoon gem to try to add these resource on to a sigle edit/new form (the Stuff one). The problem is that when I try to update the stuff with a new group (with new labels in it), it says this Labels lables labelable must exist . I think that that's an error given becouse the first lable(group) is not yet saved to the database, so it can't give his nested lable an id. (not sure though) Label Stuff Stuff Labels lables labelable must exist Also, is this the best way to do that? I mean, I made the label polymorphic only because I needed to save a only a string, and it would be unpractical and it would have taken database storage for nothing... Enough talking, here...