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.

learn more… | top users | synonyms (1)

1
vote
1answer
39 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 ...
2
votes
0answers
37 views

Which version of custom mysql sql duplicate_key update query is cleaner?

The goal is to create an activity or update it's counter if it is already present. And to be super accurate the code avoids race conditions with a custom ...
1
vote
1answer
39 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
33 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 ...
2
votes
1answer
37 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
0answers
31 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
44 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
49 views

Show all records when current user is admin

I have the following. How best to refactor it: ...
-3
votes
0answers
31 views

API-only e-commerce Rails app

I'm building an API-only e-commerce Rails app with lots of controllers and an admin panel. I'm thinking on how to namespace/organize my routes and controllers. The best guess I had for now is to try ...
1
vote
2answers
37 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
27 views

Classes for attributes validation

I have two classes responsible for attributes validation: ...
1
vote
0answers
11 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
48 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
36 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
27 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
40 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
43 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
58 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
33 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
72 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
66 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
65 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
69 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
97 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
62 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
77 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 ...
2
votes
1answer
74 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 ...
2
votes
0answers
50 views

Modeling a temperature recording device in Rails

The app will allow users to login and upload a CSV file that contains temperature data from a device. This data will be stored and the user can view all these downloads and select one from a list to ...
2
votes
2answers
62 views

Average days on market calculation

I have the following method which calculates average days on market for a sale type segment: ...
2
votes
1answer
47 views

refactor helper for default state for rails select with enums

I've created a helper method that is essentially used to get the default state of an select: ...
4
votes
1answer
81 views

Using Rails concerns for validation in models

This is my first Rails project. I have read about concerns which are a great tool, but I am confused about how I should redesign my models. Some models have similar fields, but the validations are ...
4
votes
2answers
196 views

Checking for valid date range in Rails

I am getting a date range from form inputs - the "from" month, day, and year, and the "to" month, day, and year, and then running a report on data for within that date range. I want to first check ...
1
vote
2answers
36 views

Finding the object and the URL associated with a notification

I have a Notification model which inherits from the ActiveRecord class. Each notification is associated with a specific type of action, which is determined by an integer field (...
5
votes
3answers
159 views

Avoid printing attributes of the nil object in Rails

Currently, when I'm wanting to print a value for my class I do the following... <%= @person.team.name if @person.team.present? %> This seems really ...
6
votes
1answer
148 views

My first React.js components for a golf app i'm building, how did I do?

So I've recently started to look into react, I feel like I'm understanding it, but I want to make sure I'm doing the best practices before I make too many bad habits. I'm using the ...
1
vote
1answer
29 views

Is dynamically generating routes from Model enum safe?

I am generating routes and responding to it dynamically by looking at the enum defined in the model. Is this safe and is there more proper method? ...
2
votes
1answer
33 views

Authentication and session creation

My session controller has a method for creating new user session. According to Rubocop's output there is 'Assignment Branch Condition' metric is too high [15.17/15]. ...
1
vote
1answer
58 views

Filter to convert duration to hours — in controller or model?

I have this before_action method : ...
3
votes
1answer
70 views

Rails validating API parameters

I wanted to keep my API request logic separate form the controller logic. I therefore make use of a separate model (EmailChecker) that creates an instance of this ...
3
votes
1answer
49 views

Table rendering helper

I wrote this little helper to render a table, however, working with nested content_tag is messy. ...
4
votes
3answers
84 views

Setting locale based on preferences with fallbacks

Consider the following code. Are the multiple assignments to the loc variable a code smell? If so, how can I refactor this? ...
1
vote
0answers
40 views

Copying csv to sql_table

I wrote code that takes csv and export it to sql table. It looks like this: ...
3
votes
0answers
69 views

Multiple many-to-many filter with range using filteriffic

I should say that the code I have works. I feel like there has to be a much more efficient way to do it (especially when I just crashed my server with it). Relevant schema: ...
1
vote
1answer
40 views

Checking whether users have a role that allows them to index sheets

Is there any DRYer way to write this method ? or is it better to keep it this way for better reader understanding ... Each role has a list of authorised activities for a specific resource ( in this ...
0
votes
1answer
131 views

What's the best name for a service object in Rails?

One challenge I regularly face is how to name my service objects in Rails. I like the advice in this blog post on Gourmet Service Objects, a service object performs one action. It holds the business ...
1
vote
1answer
45 views

One hash with params populate two objects

I have a JSON API where a user can update a post and related details to that post. To simplify the process the user just fills in one hash and we don't expose our architecture. What I did here works ...
2
votes
1answer
156 views

Merge two arrays of hashes and map them

I would like to refactor that code. ...
7
votes
1answer
55 views

Summing the size/count of associations for multiple records

I'm working on a plain Ruby class within a Rails application. For the total_leg_count method---is that the best way to go about achieving what I want? I feel like ...