Ruby on Rails is an open-source web development framework optimized for programmer happiness and maintaining sustainable productivity. Many things are defined by convention, freeing you from having to re-invent things to stay productive.
5
votes
1answer
59 views
Computing overall averages in my web application
I'm a senior Comp. Sci. major working on a senior design project for our faculty. The name of this project is "Gradebook", and it is responsible for allowing instructors to record grades for students ...
3
votes
1answer
52 views
Probability ordering by seller's tariff
I have an instance method (update_priority); it calculates the position of an element, depending on the tariff of its seller.
If the seller is of a level3 tariff, ...
1
vote
1answer
40 views
Find free username based on schema
Hopefully this is not too complicated. I have this code that will generate a username based on the pre-configured schema the admin types in. The fields they can use are:
...
0
votes
2answers
31 views
Testing a simple guard clause in isolation
I’m using decorator objects in a Rails app so that I don’t have to do nil? checks in views. Everything is working nicely and it’s easy to test, but I’m suspicious ...
0
votes
0answers
9 views
2
votes
3answers
48 views
Similar methods to update today's sales, revenue, and profit
Please help me to refactor. I have two models, conversion & statistic.
Methods ...
1
vote
1answer
35 views
Rails integration testing: Should we use strings or named routes in http requests?
When integration testing Rails applications HTTP request helpers can take a string or a named route as the first argument.
I find myself writing:
...
3
votes
0answers
39 views
Rails models for Users, Offers, Comments, Documents, and Reviews
I want to simplify my Rails models, current looks like this:
...
4
votes
1answer
53 views
Rails configuration variants for production / development environments
How can duplication be removed from this?
...
4
votes
1answer
55 views
Strategy to reduce duplicate code in many similar modules
The Situation
I have created some code in the form of modules that each represent a medical questionnaire (I'm calling them Catalogs). Each different questionnaire ...
3
votes
3answers
80 views
3
votes
2answers
51 views
Simple DRY Rails - check if a model belongs to a user
I can clearly see I'm repeating myself, but I don't know how to refractor this without making it really ugly. What I have:
...
2
votes
2answers
34 views
Product matcher refactoring regarding scopes and arrays
I know I have seen this before and I can't remember how to fix it or find where I saw how to fix it:
...
1
vote
1answer
22 views
The block should return the user's current_active_role
def current_active_role
user_roles.each do |r|
if r.is_active_role
return r
end
end
return nil
end
The block above returns ...
0
votes
1answer
35 views
Refactor Net::SFTP Upload
I have a method to upload a file using the Net::SFTP gem. My method looks like this:
...
1
vote
0answers
18 views
Joining models to get records
Is there a better way to refactor the join and fetch the required attributes or is there any better option here?
...
3
votes
1answer
55 views
Reading a file in chunks
I currently have the following class that takes a custom file object as an argument and makes a copy of the file locally.
...
2
votes
1answer
34 views
Displaying shows with videos
My method takes a long time to respond, so I'd like to make this method more efficient. I'd also like this method made with joins.
...
4
votes
2answers
58 views
Convert string array to object with true/false flags
Currently a filter parameter of a GET request is used to designate items' categories that will be requested from database and displayed.
The parameter contains ...
0
votes
2answers
44 views
3 features for 1 model = 3 migrations?
I have a model Users that each instance have 3 features. It was alright until I wanted to tell if this instance can do the 1st feature, the 3rd or all.
I did a migration
...
7
votes
1answer
64 views
Controller action to process orders and payments
I'm trying to reduce the if statements and simplify this create action.
...
3
votes
4answers
84 views
Tell, Don't Ask when dealing with displaying model attributes or a null replacement
I have the following model:
...
1
vote
1answer
58 views
Triggering the execution of a job at the end of a timed campaign
I have a task that checks for campaigns that are supposed to be finished by checking the end_at date. If finished, then set the status of the campaign to finished, ...
2
votes
1answer
63 views
DRY-ing up some rspec
I have an rspec spec test that has two sets of the same tests (from 24 Pull Requests). I thought I'd refactor the spec whilst I fixed something else.
Normally I just do a basic ...
4
votes
2answers
55 views
Rendering a selection box for 50 states
I was creating a Rails form where users have to enter their state. I ended up creating a select tag which is really, really ugly.
I'd like to think there's a better way of writing this.
...
3
votes
2answers
70 views
Reservation validation
I got a Reservation model that has reservation_start and reservation_end.
Then I have the ...
1
vote
1answer
57 views
Reduce memory use for a property geolocator
I have to run a script against 40 million objects in my database:
I wrote the script and I want 10 instance of it so I process my objects faster.
The goal is to find all the neighborhood for each ...
0
votes
1answer
94 views
Avoiding Duplicates in a Ruby on Rails Factory Girl Factory with Fake
I have a Factory Girl factory that needs to generate a unique name using Faker. Unfortunately, Factory Girl generates duplicates relatively frequently, which causes intermittent test errors. I ...
-1
votes
1answer
62 views
Feeder for returning an array of items rated by users
Feeder is in charge of returning an array of items rated by users (feed). If there is a watching user (...
0
votes
2answers
64 views
Clean up Rails routes.rb constraints
I'm trying to think of a better way to write these routes in my routes.rb file:
...
1
vote
1answer
50 views
Getting s3 objects with Rails
I have been using aws-sdk gem with Rails, for uploading and downloading objects. I believe it's working fine.
Demo code:
...
3
votes
1answer
121 views
1
vote
4answers
57 views
1
vote
1answer
119 views
Add existing models to a relation using nested attributes
I needed to add existing models to a has-many relation using nested attributes so I overwrote thumbnails_attributes=:
...
2
votes
0answers
32 views
Directions to refactor class for GPS track points
The story started here (Parsing GPS Data) and continued here (Class for median filter)
I've done a lot work and faced new questions.
In short - I made:
Classes for parsers. TrackParser and ...
1
vote
1answer
47 views
Building query for search engine
I'm writing a basic search engine for my website.
It works but I'm really unhappy with the code. I know it can't be improved but I'm lacking some experiences here.
In my params from my search I get:
...
1
vote
1answer
45 views
Parsing GPS data
Recently I sent this code snippet as CV attachment and I got simple answer - this code smells and we employ another developer.
I'm new to RoR, so guys can you give me directions to refactor this code ...
-2
votes
1answer
121 views
SQL table: store “status” as integer vs as string [closed]
I've been around a bunch of projects where statuses are stored in the db as a integer, then mapped in the model back to a human readable string:
...
1
vote
1answer
182 views
Testing after_save hooks in Rails 4.2 with MiniTest
I've recently started working on a new application in Rails 4.2.0-beta2. I've been using this as an opportunity to learn more about MiniTest, with a stretch goal of being as strict with myself with ...
1
vote
1answer
37 views
DRY up ivar assignment in Rails new and create controller actions
When creating a new model in Rails, I need to pass a bunch of other models into the view so I can define the correct associations. All I’m really doing here is querying the persistence layer for ...
6
votes
2answers
76 views
How to avoid mounting SQL by concatenating strings?
I found myself mounting a query by concatenating strings. This just seems wrong. Any ideas of how to avoid this kind of situation?
I'm using Rails 4, and I don't see how can ...
3
votes
2answers
47 views
Initialise simple_form object inline
Using Rails 4 and a simple form, I'm rendering a couple of forms outside of their native controllers. Rather than scattering initialisers through my controllers, I wondered if it is cleaner to do the ...
0
votes
2answers
70 views
Make multiple conditions in if expression more readable
My logic has to be written in this way, but I think the if expression is too long to read.
How can I make it more readable?
...
2
votes
1answer
47 views
View for displaying nounal and verbal pages
I've got this code in my View that just list two type of links.
Here's an example output:
Nounal Pages
cleaners/house-cleaners
Verbal Pages
cleaning/house-cleaning
Here's the view code. How ...
3
votes
1answer
78 views
Searching for something at a location
I'm using Searchkick in a situation where a user might write the location they are searching for, or the thing they are searching for, or both.
I've been working on the below code for most of the ...
2
votes
1answer
93 views
Ruby on Rails Method ActiveRecord Query Optimizations
So I have three methods and I'm not sure how to optimize them. I will be working on this and updating this post while I do but basically running these three methods is central to some parts of my ...
1
vote
0answers
97 views
Rails Controller vs Service Object for Application Logic
I have a #change_account action in the accounts controller that verifies if the user has access to the requested account prior to changing the current_account which happens via setting the ...
2
votes
0answers
126 views
DRY my rake import from external database tasks
The application I'm working on runs import tasks each night to get information from an external database and processes and writes it to the local PostgreSQL database. This is so it can be viewed in ...
0
votes
1answer
16 views
Model with many associations produces code complexity
I've refactored this model about 50 times according to codeclimate, trying to get the codesmell down. With all of the associations, I feel like this is as bare bones as I can get it while still ...
5
votes
2answers
351 views
Rails controller method that conditionally filters results
This code for my blog checks to see if tags are in the params hash. If they are, then only posts that are tagged will be paginated. Otherwise, all of the posts are paginated.
...