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
3 votes
1 answer
339 views

Ruby wrapper for Instagram API

I've created my first gem as Instagram API wrapper https://github.com/agilie/instagram_api_gem. I would be very pleased if you give me some feedback on code organization, methods elegancy and so on. ...
Sergey Mell's user avatar
4 votes
7 answers
3k views

Display related records, if they exist, in Rails view

Is this much logic in view is justified to check for the presence of record? ...
Rajkaran Mishra's user avatar
1 vote
0 answers
1k views

Unit-testing authorization policies in Rails with RSpec

The following specs test the authorization setup of the Post resource in a Rails 4.2 application. The Pundit gem is used for authorization but I believe the specs ...
BrunoF's user avatar
  • 155
1 vote
3 answers
4k views

Count has_many relationships

I have 3 models: Import, Product, Values. An import has_many products. A product has_many values. I want to find the product with the highest values count and the sum of all values for all those ...
Graham Slick's user avatar
2 votes
1 answer
605 views

URL whitelist class

I'm trying to make a class for safe redirection of URLs. Redirecting URLs is something you can have in a get parameter, for example: ...
Roman Pushkin's user avatar
3 votes
2 answers
78 views

Rails method to match users according to language fluency level

This code works fine, but I'm still learning, and feel like there is a more Railsy way to write this. Is there a good rails ActiveRecord method I should learn more about? Please let me know if you ...
gwalshington's user avatar
3 votes
2 answers
65 views

Persist relation to a class (and not a class instance) in Ruby on Rails with Mongoid

Assume my users can subscribe to several plan_types. I want to define those plans as classes, and be able to keep, for each user, a reference to a subscription_plan and also a presubscription_plan. ...
Cyril Duchon-Doris's user avatar
0 votes
1 answer
48 views

Checking whether any of some large collection of attributes has changed [closed]

I have to check a large number of attributes and do something if any of them have changed. However, right now there are way too many or statements. Is there a better way to do this? ...
helpmeplz's user avatar
1 vote
2 answers
2k views

Setting unique code to model before creating (ruby/rails)

I have a Table model that needs a unique code. I have a method creating the code (and making sure it's unique), but I have the same line ...
Mirror318's user avatar
  • 205
0 votes
1 answer
113 views

List User Online Status

I'm using Rails with MongoDB for database. In my apps I have 1 action for displaying list of users. User has name, image, ...
itx's user avatar
  • 269
1 vote
1 answer
140 views

Ruby on Rails Messaging board

I would definitely love some feedback on this message board app I made. I been doing front end development for the most part and I just want to keep my backend chops up. Let me know anywhere that can ...
catch22's user avatar
  • 113
0 votes
2 answers
4k views

Cyclomatic complexity for method is too high

How do i refactor my code to fix the lint error i am getting? ...
jose's user avatar
  • 9
2 votes
0 answers
635 views

Module to dynamically generate controller concerns

I'm rebuilding a Rails project from when I started developing, and I wanted to figure out how to really DRY my controllers out—without making them unreadable by doing half/all of the work in a ...
Phillip Longman's user avatar
2 votes
2 answers
87 views

Build Hash with 2 input variables

I have a case that looks like this : John has 120 candies, He needs some plastic bags and marks each plastic bag with the number and puts 50 candies in each bag. So John can only fill up to 3 plastic ...
itx's user avatar
  • 269
3 votes
1 answer
185 views

Rails 5 controller to get information about Steam users

I've done a bit of modifications and finally got it working, and did a lot of reading about @ vs @@ vs just declaring and I came ...
DNorthrup's user avatar
  • 285
3 votes
2 answers
719 views

Seed a database

I'm looking for advice / best practice to avoid doing a deeply nested set of IF/THEN statements in Rails. The intention is to ...
Mirv - Matt's user avatar
4 votes
2 answers
55 views

Retrieve next card(s) base on the provided parameter

How can I remove the duplicated params[:next_cards]? ...
XYZ's user avatar
  • 167
2 votes
2 answers
791 views

Update single common column in multiple has-many relationships rails

I want to update a single column in 4 tables. All relationships are has-many among them. Private schools has many private classes Private classes has many lesson plans Lesson plans has many quizzes ...
LearningROR's user avatar
2 votes
0 answers
30 views

mapping event uuids to events in ruby

I have quite an interesting challenge for you today. The following code will produce a map with events as values and event uuids as keys. I've also listed out the spec for it. I would like to know ...
Calin's user avatar
  • 121
2 votes
1 answer
587 views

Handling ajax responses in Rails

I have the following method which creates a like for a post: ...
Cameron's user avatar
  • 345
2 votes
2 answers
3k views

Rails like and unlike posts

I have the following two methods which handle the liking and unliking of posts in my Rails application: ...
Cameron's user avatar
  • 345
0 votes
1 answer
718 views

Displaying job status in a jQuery Datatable

I was wondering if there was a better way to write this: ...
Wes's user avatar
  • 133
3 votes
1 answer
3k views

Array of date by each week

I have two dates, example : date1 = "4-02-2017".to_date date2 = "4-02-2017".to_date + 29 I try to get array of date by each week with this : ...
itx's user avatar
  • 269
2 votes
1 answer
64 views

Displaying model statistics (as a sentence) in a view

A Rails 4.2 app has three models: User Author Quote ... and the following associations: users HABTM authors author has_many quotes If the user is associated with any authors, his "profile" view ...
BrunoF's user avatar
  • 155
2 votes
1 answer
213 views

Listing employees, categorized by license expiration date

I am working on a personal project and I have hit a wall. I know I am writing bad code and I really want to refactor the code below. The application has three tables on the same page. Each table ...
Benjamin's user avatar
1 vote
1 answer
222 views

Use an existing invoice if one exists, else create one

Here are the requirements: If initial_invoice? is false, existing_invoice always exists. If ...
Peco's user avatar
  • 125
1 vote
1 answer
94 views

Rails Calendar Generation Method

I have this helper method that generates an HTML calendar based on the current day: ...
Mohammad's user avatar
  • 1,212
2 votes
1 answer
4k views

Replacing the vowels of a string with the letter "X"

This code replaces every vowel of the string with "X": ...
Joana B's user avatar
  • 51
1 vote
1 answer
54 views

Create a SelfJoin with has_many

I am trying to do a self-join. I generated this migration to make a correct primary. This code works, but I'm not sure if this is the best way to do things. ...
m26a's user avatar
  • 231
5 votes
2 answers
1k views

Rails API for communication between two Rails app where the former app posts data to the latter and the latter return a response

The Controller code for the first app to post data to another app ...
Mahesh Mesta's user avatar
1 vote
2 answers
114 views

Class/model hierarchy for a school-management application

Please find how I would model a system that had teachers, students, classes and parents. Instead of creating a model for each user type, I created only two models, ...
robskrob's user avatar
  • 111
0 votes
1 answer
70 views

Validating only attributes that are present in a form [closed]

I want to skip validations based on attributes. If certain attributes are not in form so I do not want to validate them. Currently, if I post them from one form which has 1 field, it still validates ...
LearningROR's user avatar
0 votes
1 answer
83 views

How to run this code faster

This sample code is correct and no error however this only good if you only have few hundreds or thousand datas but if you have million queries this takes time how we can improve this algorithm ...
Ruel Nopal's user avatar
3 votes
2 answers
312 views

Build a pizza and choose your toppings

I've created a scenario below, in which a user can build a pizza and choose their toppings, then order their pizza: ...
Kori John Roys's user avatar
3 votes
1 answer
75 views

Helper that swaps out some text for certain icons

I have a Rails helper that swaps out some text for certain icons. Example: Helper Sample ...
DNorthrup's user avatar
  • 285
1 vote
1 answer
53 views

Finding users who liked a post

I have a method liked_by and it doesn't seem like appropriate as per Ruby style. I need to do it as less as possible. ...
Damir Nurgaliev's user avatar
2 votes
0 answers
478 views

Scoping through embedded document in MongoId

I'm using MongoId in a Rails project. I need to select some products which contain some SKUs which has an embedded document. The models are respectively Product and ...
Laurent's user avatar
  • 203
2 votes
0 answers
59 views

MySQL Workbench RoR Migration Exporter

I created plugin for MySQL Workbench that allows you to export an ER Diagram to RoR migration files. I would like to know if any other developers appreciate it. export-rails-4-migrations_grt.py <...
user3432195's user avatar
1 vote
1 answer
60 views

Skip invalid record and avoid execution break for invalid record

Here is the pseudo code used inside a Rails controller: ...
Dinesh Saini's user avatar
1 vote
1 answer
833 views

Directed Acyclic graph implementation in Ruby on Rails

I have an implementation for a graph node class that I'd like to have function as a directed acyclic graph. The associations are roughly as follows ...
David Zhang's user avatar
1 vote
0 answers
1k views

Converting ActiveRecord array to JSON

I have two tables, devices and device_properties. The devices table columns are as follows: ...
Dobz's user avatar
  • 111
1 vote
2 answers
5k views

Set ENV variables for Ruby on Rails

I've followed the steps from this video from railscasts for setting ENV variables. This is my configuration: config/database.yml ...
ltdev's user avatar
  • 113
3 votes
1 answer
79 views

Price rating approach

I'm pretty new to web development and I don't know if my solution is OK according to the best practices, considering performance and clean code. I have a Merchant model that belongs to a Price model. ...
user1301037's user avatar
3 votes
1 answer
1k views

Rails form helper to display email fields and their validation errors

I have a repeated pattern throughout my app that looks like this: ...
Daniel Viglione's user avatar
2 votes
1 answer
53 views

Where should I put code which calculates stats which will be sent via mail

I want to send mail which will have day to day report. I have setup action mailer like this ...
dnsh's user avatar
  • 133
-5 votes
1 answer
54 views

Getting last 5 leave requests for a user - where does the code live [closed]

I have a User model, a user has_many leave_requests, in the user model I have the following code to find the last 5 leave requests: ...
Craig McGuff's user avatar
5 votes
1 answer
474 views

ActiveAdmin: Hide a whole panel in a smart way

In order to only show relevant information to my users I want to disable the whole panel when all fields inside that panel are empty. Currently I do this hacky method to get to this result: ...
Besi's user avatar
  • 151
1 vote
2 answers
2k views

Finding mongodb records in batches using mongoid [closed]

I want to get records in batches. But mongoid doc says to avoid using skip due as it can be expensive. I wrote this method to iterate through a large number records efficiently. ...
tessie's user avatar
  • 315
3 votes
1 answer
201 views

Imported data from CSV successfully, but slow

Currently I am using the gem activerecord-import to seed more than 55,000 records into my database, and it works fine. However, it takes too much time. I am ...
Fatima's user avatar
  • 143
4 votes
3 answers
2k views

Loop through months (1-12)

I currently have this monstrosity: next_period = (((current_month + tax_period_months)-1) % 12) + 1 I want to get the month that is ...
Mirror318's user avatar
  • 205

1 2
3
4 5
12