Posts

Showing posts with the label ruby-on-rails-5

Autoload paths and nested services classes crash in Ruby

Image
Clash Royale CLAN TAG #URR8PPP Autoload paths and nested services classes crash in Ruby I've multiple issues to load / require classes under my app/services folder in a Rails 5 project and I'm starting to give up on this issue. app/services First of all and to be clear, services/ are simple PORO classes I use throughout my project to abstract most of the business logic from the controllers, models, etc. services/ The tree looks like this app/ services/ my_service/ base.rb funny_name.rb my_service.rb models/ funny_name.rb First, when I tried to use MyService.const_get('FunnyName') it got FunnyName from my models directory. It does not seem to have the same behavior when I do MyService::FunnyName directly though, in most of my tests and changes this was working fine, it's odd. MyService.const_get('FunnyName') FunnyName MyService::FunnyName I realised Rails config.autoload_paths does not load things recursively ; it would makes sense that t...

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...

In Jbuilder, How Can you Generate an Array of IDs Without the 'ID' Key?

Image
Clash Royale CLAN TAG #URR8PPP In Jbuilder, How Can you Generate an Array of IDs Without the 'ID' Key? I have the following Jbuilder setup but I'd like to output the 'uw_question_ids' as values only. No 'id:' key. Is this possible? json.menu do json.uw_question_ids menu.uw_questions do |uw_question| json.(uw_question, :id) end end Currently, the output JSON is... "menu":{"uw_question_ids":[{"id":"17"}]} I'd like it be... "menu":{"uw_question_ids":[{"17"}]} 1 Answer 1 I figured it out... simple! json.menu do json.uw_question_ids product.uw_question_ids end I didn't realize that Rails gives you access to a 'collection_singular_ids' method (in my case, 'uw_question_ids') when you create has_many or has_and_belongs_to_many associations. That's what I needed. ...

Best practice regarding controllers for a resource that can have different parent classes

Image
Clash Royale CLAN TAG #URR8PPP Best practice regarding controllers for a resource that can have different parent classes I'm currently trying to refactor some controller code and I came accross some code that I'm not sure how to implement in a correct way. My application has users and companies, and both can have projects. The current situation is that we have 2 urls: Those will route to the same controller which will handle the request differently based on if a company_id exists or not. This is not very clean in my opinion so I have been thinking about a better way to do this. So far, I think the best way is to split them up in seperate controllers, like: But since the only difference between a user project and a company project is pretty much that one has a 'user_id' and the other has a 'company_id', it feels like that will not be very DRY as I'll be writing a lot of duplicate code. The current solution probably isn't as much of a problem, but I want t...

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...

display results with a show more link

Image
Clash Royale CLAN TAG #URR8PPP display results with a show more link I'm trying to figure out a way to always show 5 jobs in my Rails app and then have a link that when clicked will show all of the remaining jobs. Should I do something in my jobs_controller where I'm just getting the first 5 and then getting the rest, or is it better to do it in the view somehow? <!-- company --> <div class="vertical-space"> <b>Company Name</b><br /> <div class="sub-text"> <% Job.by_company_count.size.each do |name, count| %> <div class="indent"><%= link_to name, filtered_jobs_path(company: name) %> (<%= count %>) <br> </div><!--./indent--> <% end %> </div><!--./sub-text--> </div> By clicking "Post Your Answer", you acknowledge that you have read ou...