8
votes
Accepted
Finding users who liked a post
Use joined tables to make more performant database calculations and avoid unnecessary models instantiations.
...
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 ...
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 ...
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 ...
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 ...
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
...
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:
...
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:
...
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 ...
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:
...
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 ...
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 ...
3
votes
Accepted
Helper that swaps out some text for certain icons
You can create the entries programmatically:
...
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
...
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 ...
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 ...
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 <...
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 ...
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 ...
3
votes
Accepted
URL Routing Application
You could do the following. (I assume you already have a resourceful route for your documents right?)
...
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 ...
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 ...
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:
...
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 ...
3
votes
Ruby CLI TicTacToe
Welcome to Code Review, you have almost the same code block repeated 9 times:
...
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 ...
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
...
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 }
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
ruby-on-rails × 581ruby × 554
active-record × 74
controller × 29
rspec × 27
performance × 26
beginner × 26
validation × 20
datetime × 18
unit-testing × 17
api × 15
mvc × 14
html × 13
form × 12
postgresql × 11
object-oriented × 10
sql × 10
database × 10
javascript × 9
comparative-review × 9
mysql × 8
error-handling × 8
jquery × 7
design-patterns × 7
url-routing × 7