Skip to main content

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
2 votes
0 answers
82 views

ActiveAdmin menu-generating mixin

I've written an ActiveAdmin mixin, such that you can include it and call simple_menu_for Foo, to reduce duplicate code when working with simple models. Here is the ...
Mike Belyakov's user avatar
2 votes
0 answers
462 views

Custom bang! method and Rails validation

I have a model called booking. ...
lulalala's user avatar
  • 121
2 votes
1 answer
6k views

Refactor code to solve the 'Line is too long' error in rubocop [closed]

Method.create(domain_id: domain.id, type_notification: type, message: title, url: url, owner_id: self.user.id) How can I reduce this line to 80 ?
Shilpi Agrawal's user avatar
8 votes
2 answers
217 views

Error-testing be with cucumber/capybaraweb

I'm working on writing tests for a rails application using cucumber and capybara. I have a scenario for a user editing a post, and making it invalid. The scenario looks like this: ...
user2367593's user avatar
1 vote
1 answer
48 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 tests?...
CheeseFry's user avatar
  • 145
3 votes
2 answers
110 views

Ruby method that is validating the payload received

What is a cleaner and/or clearer way of writing the following method ...
CheeseFry's user avatar
  • 145
2 votes
1 answer
72 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 ...
Ramsy de Vos's user avatar
2 votes
3 answers
105 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 ...
Szilard Magyar's user avatar
1 vote
2 answers
108 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. ...
user avatar
2 votes
1 answer
121 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 ...
VoidZero's user avatar
5 votes
1 answer
379 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 ...
TemporaryName's user avatar
3 votes
1 answer
47 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: ...
Graham Slick's user avatar
6 votes
1 answer
126 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 ...
ROR learning's user avatar
1 vote
1 answer
254 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, ...
intercoder's user avatar
5 votes
3 answers
8k 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 ...
MicFin's user avatar
  • 163
3 votes
1 answer
906 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-...
Andrew Breunig's user avatar
2 votes
1 answer
394 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? ...
DelGiudice's user avatar
1 vote
1 answer
56 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 ...
Tony Gaeta's user avatar
2 votes
1 answer
352 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 ...
Amanda Dolan's user avatar
1 vote
1 answer
470 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 ...
Tommy K's user avatar
  • 153
2 votes
2 answers
176 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 ...
Tony Gaeta's user avatar
3 votes
0 answers
40 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 ...
Joe Masilotti's user avatar
1 vote
1 answer
54 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,...
Daniel's user avatar
  • 177
3 votes
0 answers
118 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 ...
user1035375's user avatar
1 vote
1 answer
2k 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 ...
Thierry M.'s user avatar
3 votes
1 answer
2k 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 ...
Mohamad's user avatar
  • 2,034
5 votes
1 answer
79 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 ...
Ramsy de Vos's user avatar
3 votes
0 answers
70 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. ...
Filipe Chagas's user avatar
2 votes
1 answer
348 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 ...
just so's user avatar
  • 237
3 votes
2 answers
354 views

Show all records when current user is admin

I have the following. How best to refactor it: ...
Victor's user avatar
  • 321
1 vote
2 answers
157 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 ...
GhostRider's user avatar
3 votes
1 answer
40 views

Classes for attributes validation

I have two classes responsible for attributes validation: ...
just so's user avatar
  • 237
1 vote
1 answer
69 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'...
LewlSauce's user avatar
  • 123
0 votes
3 answers
2k 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. ...
mdkrog's user avatar
  • 111
3 votes
1 answer
92 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 ...
Thomas Taylor's user avatar
4 votes
1 answer
63 views

Three identical AJAX button handlers in three views

In my Rails 4 app I have this event listener: ...
rrcdr's user avatar
  • 51
5 votes
2 answers
784 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 ...
just so's user avatar
  • 237
3 votes
1 answer
133 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 ...
Boss Nass's user avatar
  • 131
7 votes
2 answers
295 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 ...
dimid's user avatar
  • 303
6 votes
1 answer
409 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 ...
Daniel's user avatar
  • 177
1 vote
1 answer
98 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 ...
yellowreign's user avatar
2 votes
2 answers
97 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] ...
Pavel's user avatar
  • 123
4 votes
2 answers
2k 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 (...
Thomas Taylor's user avatar
3 votes
3 answers
121 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: ...
Mio's user avatar
  • 153
5 votes
2 answers
562 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 ...
Jack's user avatar
  • 51
4 votes
2 answers
64 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 ...
Sebastian Jennings Almnes's user avatar
5 votes
1 answer
113 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 ...
Marc-Andre's user avatar
  • 6,711
4 votes
1 answer
4k 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 ...
Ben Hawker's user avatar
2 votes
0 answers
60 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 ...
Kazi's user avatar
  • 21
2 votes
2 answers
78 views

Average days on market calculation

I have the following method which calculates average days on market for a sale type segment: ...
Thomas Taylor's user avatar

1
3 4
5
6 7
12