Questions tagged [ruby-on-rails]

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.

Filter by
Sorted by
Tagged with
1
vote
0answers
58 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 it....
6
votes
1answer
116 views

Seeding a database

I'm seeding my database with a relatively complex data model. Constraints: Every meal has exactly one main dish (marked as is_main in intermediate table). Here is ...
1
vote
1answer
137 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, ...
5
votes
3answers
5k 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
1answer
825 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 bus-...
2
votes
1answer
283 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
53 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 ...
2
votes
1answer
230 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
394 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
141 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
35 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
52 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 reason,...
3
votes
0answers
115 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
1k 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 to ...
3
votes
1answer
1k 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 ...
5
votes
1answer
76 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
64 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
332 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
196 views

Show all records when current user is admin

I have the following. How best to refactor it: ...
1
vote
2answers
140 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
34 views

Classes for attributes validation

I have two classes responsible for attributes validation: ...
1
vote
1answer
61 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 I'...
0
votes
2answers
1k 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. ...
3
votes
1answer
73 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
54 views

Three identical AJAX button handlers in three views

In my Rails 4 app I have this event listener: ...
5
votes
2answers
499 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
92 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 ...
7
votes
2answers
218 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 ...
6
votes
1answer
339 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
90 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
91 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
1k 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
114 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
458 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
60 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
107 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
3k 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
58 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
76 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
175 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: ...
6
votes
1answer
2k 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 ...
5
votes
2answers
8k 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
45 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 (...
7
votes
4answers
3k 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
523 views

React.js components for a golf app

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
291 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
49 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
100 views

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

I have this before_action method : ...
5
votes
2answers
3k 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
683 views

Table rendering helper

I wrote this little helper to render a table, however, working with nested content_tag is messy. ...

1
3 4
5
6 7
12