Skip to main content
8 votes
Accepted

Finding users who liked a post

Use joined tables to make more performant database calculations and avoid unnecessary models instantiations. ...
mikdiet's user avatar
  • 301
6 votes

Replacing the vowels of a string with the letter "X"

Look into regular expressions, which Ruby is very good at: def vowel_censor(string) string.gsub(/[aeiou]/i, "X") # case-insensitive end Or, even easier, more ...
Flambino's user avatar
  • 33k
6 votes
Accepted

Rails Helper to display rating in stars

You can make use of the Array.new, passing in the maximum number of stars you want to show, and defaulting all the stars to empty. Then, you can ...
Simple Lime's user avatar
6 votes
Accepted

How can I make this CSV Importer code better of any code smell?

I like that you already extracted a WebCsvImporter Plain Old Ruby Object which makes the code already a lot easier to understand and refactor. Here is a refactored ...
Christian Bruckmayer's user avatar
5 votes

Rails API for communication between two Rails app where the former app posts data to the latter and the latter return a response

I will try to approach this from top to bottom, and mention things as I notice them. (1) First obvious one is indentation, as mentioned in the other answer. The standard is 2 spaces. Nested modules ...
Damien's user avatar
  • 335
4 votes

Rails API for communication between two Rails app where the former app posts data to the latter and the latter return a response

Code formatting The code shown in the question sometimes uses two spaces, sometimes four. Prefer two spaces for indentation; this is a a universal Ruby standard. Business logic in the controller ...
Wayne Conrad's user avatar
  • 3,284
4 votes

How to run this code faster

You have a lot of Code repeated one of the important rules is to be DRY(Don't repeat yourself), check the following, what i did is only DRY the code: ...
mohamed-ibrahim's user avatar
4 votes
Accepted

Retrieve next card(s) base on the provided parameter

Ok, in response to the conversation in comments, Here's my suggested first step: ...
Chris Drappier's user avatar
4 votes
Accepted

Group array by sums

You can use chunk, generating a new "chunk id" each time your accumulating sum overflows its max. chunk abstracts away all the ...
Jonah's user avatar
  • 4,350
4 votes
Accepted

User and picture models

You could do this with just one query. There are variations how to construct the query but I would probably do something like this: ...
Kkulikovskis's user avatar
4 votes
Accepted

Presenter pattern implementation

At this point we can already see that CartPresenter (as others) needs to embed Model's data into HTML. As it violates the Single Responsibility Principle, do I need something like Decorators to take ...
Christian Bruckmayer's user avatar
4 votes
Accepted

Ruby SFTP client

... under the terms of the GNU General Public License ... IANAL, TINLA. As copyright holder you chose to license the code in your posting under the terms of CC-BY-SA, which is incompatible with GPLv3 ...
J_H's user avatar
  • 34.8k
3 votes
Accepted

Helper that swaps out some text for certain icons

You can create the entries programmatically: ...
Jonah's user avatar
  • 4,350
3 votes

Find associated object if exists, otherwise generate it

Expanding on @jpemberthy: Shouldn't we duck type? def pick_for_game(game) game = game.id if game.respond_to? :id picks.find_or_initialize_by_game_id(game) end ...
Christopher Oezbek's user avatar
3 votes
Accepted

Use an existing invoice if one exists, else create one

The refactor goal was to make clear the logic for getting an invoice. THIS IS THE MOST IMPORTANT POINT: Original code tells me there are 2 kinds of invoices so I knew I wanted a "if" that stated very ...
radarbob's user avatar
  • 7,889
3 votes
Accepted

Displaying model statistics (as a sentence) in a view

I'd add a counter cache to Author, i.e. quote_count just to avoid doing the extra query to fetch the number of quotes. Don't ...
Flambino's user avatar
  • 33k
3 votes
Accepted

Rails like and unlike posts

You're right. The code can be improved. Here is how I'd write this. We can extract post lookup into a method or before hook. There is no need to check if Post was found. In production environment <...
cutalion's user avatar
  • 184
3 votes

rails method for transaction type

It looks like row is simply a hash, but it has various value combinations that are significant. You could refactor the detection of those combinations, for example ...
David Aldridge's user avatar
3 votes
Accepted

A method that calculates win streaks

You can use Array#min and Array#max to calculate the win/lose streak; You don't need to use self on getters; And you can use ...
Peoplee's user avatar
  • 205
3 votes
Accepted

URL Routing Application

You could do the following. (I assume you already have a resourceful route for your documents right?) ...
siegy22's user avatar
  • 146
3 votes

Validate if the user is associated with the organization then it must have any valid timezone

In case if you wanna improve only the validate_time_zone method: Performance wise I don't see anything sinister in your implementation. I assume the ...
Peter Toth's user avatar
3 votes
Accepted

Update join table using list of checkboxes in Rails

Actually, Active Record can work with joined collection (including has_many :through) from the box. So, you just need to pass new collection instead of old, and AR will delete excessive and add new ...
mikdiet's user avatar
  • 301
3 votes
Accepted

Dynamically combine ActiveRecord Relation results

Turns out the last code snippet in my question was close to what I ended up needing: ...
kaydanzie's user avatar
3 votes
Accepted

Rails: create a relationship between comment and user

Don't think your question belongs to this code review section. You would have gotten help much faster if had posted in stackoverflow instead. Anyway there are couple points you need to check. The ...
Thang's user avatar
  • 166
3 votes

Ruby CLI TicTacToe

Welcome to Code Review, you have almost the same code block repeated 9 times: ...
Caridorc's user avatar
  • 27.6k
2 votes

Check if a store is currently open

I think this business logic can be simplified to: For you to consider a store 'open' on your site, it must be open now and in 30 minutes from now. The following logic does not handle the case where a ...
melcher's user avatar
  • 141
2 votes

Duplication of enum across two models

In Rails 6, you can put the enum in a module in concerns and include it in your models. model/concerns/statusable.rb ...
ThirtyDayDeveloper's user avatar
2 votes

Avoiding Duplicates in a Ruby on Rails Factory Girl Factory with Fake

This can be accomplished now with: name { Faker::Company.unique.name }
dft's user avatar
  • 121
2 votes
Accepted

Set ENV variables for Ruby on Rails

for what you use config/application.yml instead use config/secrets.yml. It is already implemented in Rails. Than you can fetch ...
Oleh  Sobchuk's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible