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
0answers
8 views
Method to find posts in children categories
I use the gem act_as_votable to allow users to select categories they like. To get the categories a user likes:
current_user.get_voted(Category)
These ...
0
votes
0answers
8 views
Rails rspec test with mocks/stubs
I'm learning about mocks/stubs/doubles and would like to refactor the following code.
comment is polymorphic and can belong to Post, Product, etc. That's why I ...
5
votes
1answer
136 views
Method to count unread messages
I currently use this method to count unread messages for a user (self here):
...
1
vote
1answer
45 views
Rails Grape API Endpoint optional params validation in update
I have the following rails grape api endpoint in the users resource:
...
2
votes
1answer
46 views
Checking whether a store is open or not by querying over its business hours
I currently have a method that checks if the store is open. It's working nicely, however on the main page I have quite a few stores. Whom again have different locations. Each of these locations again ...
2
votes
1answer
57 views
Rails service for a series of calculations
I'm writing a Rails service in which a series of operations are performed on a record:
...
3
votes
1answer
23 views
3
votes
1answer
44 views
Redirecting to the appropriate URL for some type of notification
I have a polymorphic notification model with action attribute. When user clicks on a notification he/she should be redirected to given page the notification refers to through notifiable.
What is the ...
4
votes
1answer
64 views
Rails Sample for interview
I did a small sample application for an Interview. Since I didn't got accepted I would like your opinion on how to improve it.
The aux methods are private.
I have a ...
1
vote
1answer
61 views
Getting predicted values for a student report form
I have a situation where I need to predict the input values for a form used to create a new model. The model is a "class report" that a teacher submits for a student after their class finishes and the ...
4
votes
1answer
39 views
ActiveRecord selection, modified by optional parameters
I have an action method in my Rails controller which filters an ActiveRecord model depending on the submitted GET parameters.
Rubocop complains that Assignment Branch Condition Size is too high: ...
2
votes
1answer
160 views
Weight-loss calculator
I have a table that holds a user's weight for specific days. In my weights_controller.rb index action I can show these weights - no problem. What I also want to ...
2
votes
1answer
73 views
Rails validating enum
The Ruby on Rails framework doesn't allow you to validate enums submitted through a form in any sane manner. Evidently, enums are meant to be used to maintain internal application state, and are not ...
1
vote
1answer
97 views
Merging two hashes with the mean values of each key if both hashes exist
There are two methods, hash1 and hash2, that return hashes or nil. The hashes have the same ...
5
votes
3answers
51 views
Client categorisation based on hourly payment
I am currently working on a small internal rating system for clients to be able to prioritise my work based on what client pays best. The input data is the hourly rate they are paying and based on ...
2
votes
1answer
20 views
Polymorphic event notification model in Rails
I have a polymorphic notification model. When user gets a notification he can get on the page of the notifiable to check out what happened exactly. For instance ...
3
votes
1answer
35 views
Install ruby using rbenv in bash script
I want to write an automated bash script for installing ruby and rails using rbenv. But I am getting issue of session reload in terminal.
My bash script:-
...
5
votes
1answer
43 views
Generate dyamic 'week of' date range
I was in need of a method to dynamically generate a week range, so that the view reads something like "Week of May 16 - 22" and updates automatically. Here's the helper method I came up with, but I'm ...
5
votes
1answer
53 views
Displaying information about a ticket purchase
My code works perfectly, however, I know my approach isn't best practice practice. I would appreciate the effort of anyone who tries to explain in details on how I could refactor this code using ...
2
votes
2answers
40 views
Recursive categories fetch and view
I have categories and subcategories in my view. The code looks like this
...
4
votes
1answer
63 views
Determining whether a task is active based on many conditions
I wrote a Rails model instance method that checks many separate conditions, and returns a single boolean. It does not employ any nesting of branching structures. Each check has its own comments ...
1
vote
1answer
37 views
Old and new shouts controllers
I have a photo_shouts_controller.rb and text_shouts_controller.rb which both have almost similar implementations, and I would like to refactor it.
I have implemented a refactoring but I am not too ...
1
vote
2answers
35 views
Rails create create default database records
In my application I have an accounts model:
class Account < ActiveRecord::Base
belongs_to :user
has_many :invoices
end
The ...
1
vote
1answer
93 views
Conversational bot architecture
I am currently building a small conversational bot, and I am struggling with the architecture. I use Facebook Messenger combined with Wit.ai's API and a Shopify store API. My rails app is the ...
3
votes
2answers
61 views
3
votes
1answer
65 views
SE-like voting system
I've created a voting system that's similar to Stack Exchange's in a Ruby on Rails web app. It's working great - as far as I can tell, all the edge cases are caught. This is the action to cast a vote. ...
0
votes
1answer
33 views
Get properties from all children in a recursive SQL structure
In our product, Company is a model that can have many Companies belonging to it; so some Companies are parents, some are children, and many are both.
We have a ...
3
votes
3answers
158 views
Check if a store is currently open
This is the dirty bastard of my current code block, and I'm trying to find a way to improve this code for increased readability, but also functionality.
The method checks if the store is currently ...
2
votes
1answer
56 views
Update record with CSV
I'm using Rails 4.2.5, and I'd like to update existing records with a csv file with two fields: id,category. Where if the category field is blank, it defaults to misc.
Here's an example csv file:
<...
4
votes
2answers
103 views
Optimize the CSV Upload function in Ruby On Rails
The code is working fine however, I would like to fix code after unless and can I store thread ID in the database too? Is there a way to improve performance?
...
2
votes
0answers
20 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 ...
2
votes
0answers
28 views
0
votes
0answers
18 views
Simple app which allows to create posts and delete them by a password
Whole thing you could find here
There is no validation, i know.
Main page:
...
1
vote
1answer
84 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 ?
8
votes
2answers
96 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:
...
1
vote
1answer
23 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?...
3
votes
2answers
42 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
42 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 ...
2
votes
3answers
78 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
84 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
37 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 ...
4
votes
1answer
116 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
42 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
38 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
31 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....
0
votes
0answers
9 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 (...
6
votes
1answer
73 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
51 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
251 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 ...
4
votes
1answer
109 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-...