Ruby on Rails is an open-source web development framework written in Ruby. If your question code is written in Ruby on Rails, tag with Ruby as the language tag. Many things are defined by convention, freeing you from having to re-invent things to stay productive.
1
vote
1answer
18 views
Mini Test testing save on one and many trips
I know there is a balance to be struck between writing dry code and damp code but I have the feeling that this is a bit too repeative. Is there a terse yet self explainatory way to rewrite these ...
2
votes
2answers
36 views
Ruby method that is validating the payload received
What is a cleaner and/or clearer way of writing the following method
...
2
votes
1answer
34 views
Count associations in a Ruby model
I'm trying to figure out if there's a better way to run the code below.
Basically Course is an association of Student and ...
0
votes
2answers
43 views
Notify owner of post and other commenters
I have a rails4 app. When a user comments on a post it should send a notification to all the guys who commented on the post and to the post creator. I have a working method in the controller, but it's ...
1
vote
2answers
76 views
Controller method to manage SMS subscriptions
I have a huge method in my controller that is not DRY and just could use some general TLC. I'm new to ruby and I have not refactored my own code before, only contrived code you find on tutorials. ...
2
votes
1answer
28 views
Changing a user status flag, with validation
I am writing a function which on update of any attribute of model sets the status of variable is_kyc_verified to false.
Here is the code of the User model and the ...
3
votes
1answer
58 views
REST API with Ruby on Rails and ReactJS
I'm currently working on building a simple REST API using Ruby on Rails. I noticed that a lot of the things I did were a bit redundant, especially the models. Just to clarify: I want this to be a REST ...
0
votes
0answers
14 views
Responsive photo gallery using Flickr API
This began as just a simple sidebar widget and evolved into what you see here.
I'm pretty happy with the results but the photos take a long time to render and sort out on the page. Is this mostly ...
3
votes
1answer
30 views
Simpler method to find users in child categories
I am using the ancestry gem to organize my post categories tree.
I have 8 parent categories, and they all have many child categories.
To retrieve an array of child categories, I use:
...
1
vote
0answers
25 views
Generating a Postgres query dealing with creating a time series
I want to sum the number of my published publications model in a given interval and return a time series. (related SO)
The main challenge is that I want the code to respect the time zone passed to ...
0
votes
0answers
8 views
Robustly send notifications from multiple processes
We have Notifications stored in a notifications table, which is worked on by multiple background workers. The notifications table has these relevant attributes:
id (primary key, unique)
send_at ...
4
votes
0answers
45 views
Seeding a database
I'm seeding my database with really complex code as I think. It works but is not good.
Constrains: Every meal has exactly ONE main dish (marked as is_main in ...
1
vote
1answer
44 views
Removing folders from my rails app with rake
I'm trying to remove the tmp/letter_opener folder. Everything works fine, just wondering if there's a better way to write it. My next step is to write some tests, ...
4
votes
2answers
138 views
Ruby method that returns an age from a date of birth
I have a method on my user model to calculate the user's age and return a human readable string. My user's can be between 1 month old and above so the returned string is different depending on if the ...
3
votes
0answers
44 views
Displaying a Google map with multiple complex markers and unique info windows
This JavaScript file is part of a large Ruby on Rails app. Any feedback is appreciated.
Update 3.7.16: as per @Jonah's suggestion, here's a description of the code's purpose:
I've built a ...
2
votes
2answers
29 views
Shipping address validations
In my journey of learning Rails, I'm wondering if I should test model validations. If so, is this a correct way to do it?
...
1
vote
1answer
45 views
Updating an auto-generated invoice (future) when current plan changes
I've built-out a mechanism to go and find an auto-generated invoice and update it with new information when a plan is changed or if users are added to the plan for the current billing period. It will ...
0
votes
0answers
26 views
Testing the integration of a complicated system
I'm trying to write tests for a recurring billing system. I want these tests to show that the functionality of the system works when put together - I have lots of unit tests, but I wanna make sure ...
2
votes
1answer
27 views
Generating sample data for a Rails application
I am working on generating sample data for my Rails application. For now we will just be using the sample data while we are designing the website. I'd like it to be as easy as possible to generate ...
1
vote
1answer
30 views
Order list of movies and save sorting in cookies
I did this project and I'm going back over it and trying to optimize some stuff. This is the movies_controller.rb file, which is basically a table that I need to sort based on user parameters. What I ...
2
votes
2answers
54 views
Checking days from now
I'm trying to respect the Ruby principle of keeping my code dry; however, in my conditional, I have different constants set to different days. But that's the only thing different. They're pretty much ...
3
votes
0answers
22 views
Updating ActiveRecord with text from a joined table
I have an Active Record update with the following data model:
TvMenuProfile has one rich_text_special
...
1
vote
1answer
46 views
Establishing parameters for a sales report, with default boolean and date values
The idea is that I am getting the params from a GET request in the form of a Hash (symbol→str) and I want to transform them into dates and booleans. For that ...
3
votes
0answers
98 views
Custom MySQL duplicate_key update queries
The goal is to create an activity or update its counter if it's already present. And to be super accurate, the code avoids race conditions with a custom ...
1
vote
1answer
51 views
Rake task to send users a reminder to post with conditions
I am implementing a feature that reminds users to make a post via email if
the user has set daily reminders to true
he has not posted yet today
and if the current hour matches when he would like ...
1
vote
1answer
54 views
Testing a Rails API controller
I'm building a JSON API. I'm writing my controller tests so that they compare the response body to actual JSON I'm generating in my tests using ActiveRecord. I wrote two helper methods that make the ...
3
votes
1answer
43 views
Checkboxes array to delete images
Basically I'm displaying an array of images on a page with checkboxes. The idea is when a user is editing a post and the checkbox is checked, the image will get deleted. Each image also has an ...
3
votes
1answer
44 views
Fat Controller Action - Deep nested filters
I have this really really long controller action and would like someone to help me break it down.
...
2
votes
1answer
49 views
Truncating article text
I need to show article's content based on this logic:
if article's content has to be truncated (e.g for preview)
if there's [read more] substring in the content
...
3
votes
2answers
53 views
1
vote
2answers
40 views
Creating a percentage complete method for a form
I am a relatively new Rails developer and doctor doing research. In my Rails based research projects I often have questionnaires for which I provide a "% complete" parameter.
Here is an example. You ...
3
votes
1answer
29 views
1
vote
0answers
13 views
Method for adding a product with product attributs to cart
Currently I have an app that have many stores. Each store can have several products. Whom again can have many product attributes assigned to them. For instance a shirt is a product, and size and color ...
0
votes
1answer
52 views
Using as few queries as possible for many table associations
I have a table containing about 30k rows of data and there's several table associations. The problem is that there's like 18 columns, and each column requires 1 db query.
Here's an example of what ...
0
votes
2answers
42 views
Duplication of enum across two models
In my Rails app, I have two classes each with the same enum for status. Code climate is complaining about duplication.
...
2
votes
0answers
30 views
Formatting a long HTML document in a model method
I was asked to make a view which is a mix of HTML & ERB logic available in multiple views. At first this seemed pretty simple. However, some of these views required the output to be plaintext ...
4
votes
1answer
31 views
Three identical AJAX button handlers in three views
In my Rails 4 app I have this event listener:
...
5
votes
2answers
42 views
Refactoring if statement based on a hash key
I have to call Article.to_draft/to_archive/publish method depending on the presence of the ...
3
votes
1answer
48 views
Ruby on rails instance loops
I am trying to use a custom sql query to display different attributes for a product i.e. Size and Price. The query I have when running in console displays as it should
...
6
votes
1answer
67 views
Script to redo a rails migration easily
I need to redo a migration occasionally. The workflow is usually something like this:
find or ls | grep the migration using ...
3
votes
0answers
35 views
Simplify the way a user retrieves scraped information
I'm allowing a user to scrape information from LinkedIn by entering the profile url. However I'm relatively new to creating a service (is that even the correct use case?) and I feel like there's room ...
6
votes
1answer
83 views
Simple authorization module with Rails
I created a simple Authorization module with Rails. I found that there are other authorization systems, such as CanCanCan, but they grant permissions at Model level and, for this particular website I ...
1
vote
1answer
69 views
Simplifying Rails Validation [closed]
I have four fields a, b, c, and d. All of them must be populated if any of them have a value.
So what I want to do is validate that they are all populated, if any of them have a value. I'm trying ...
2
votes
2answers
70 views
Rating the quality of a response based on the number of typos
I have this method which returns the number, but Rubocop said:
Assignment Branch Condition size for rating is too high. [15.84/15]
...
4
votes
2answers
77 views
Rails conditional sorting based on params
The following code sorts the current users bid model items based on a param condition :bid_status. It starts to get messy when checking if the project space (...
3
votes
3answers
98 views
Managing many availability status
I'm a junior who wants to refactor a big method in my code, which has been tested and works:
...
5
votes
2answers
83 views
Rails guestbook with new, index, and Kaminari pagination in the same method
I created a guestbook that on one page displays both the form for a new entry and all of the entries that have been created so far.
I am trying to use Kaminari for pagination for the entries.
Right ...
4
votes
2answers
57 views
Method for gathering the correct data to a specific user
I have quite a lengthy method that I know for sure can be optimized, yet I am not sure how to do it. The method is in my User model, and essentially gets the ...
5
votes
1answer
78 views
Displaying a map of points
I'm working on an HTML text-based game (think like OGame and other browser games of the genre). I've tried worked on a grid controller to display a map of points that you can move around when you have ...
3
votes
1answer
112 views
Managing users and profiles in Rails
I am building a Rails marketplace application using TDD. I would like to get advice on the way in which I have built the User associated ...