Tagged Questions

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

2
votes
0answers
21 views

Directions to refactor class for GPS track points

The story started here (Parsing GPS Data) and continued here (Class for median filter) I've done a lot work and faced new questions. In short - I made: Classes for parsers. TrackParser and ...
1
vote
1answer
21 views

Building query for search engine

I'm writing a basic search engine for my website. It works but I'm really unhappy with the code. I know it can't be improved but I'm lacking some experiences here. In my params from my search I get: ...
1
vote
1answer
37 views

Parsing GPS data

Recently I sent this code snippet as CV attachment and I got simple answer - this code smells and we employ another developer. I'm new to RoR, so guys can you give me directions to refactor this code ...
-2
votes
1answer
50 views

SQL table: store “status” as integer vs as string [closed]

I've been around a bunch of projects where statuses are stored in the db as a integer, then mapped in the model back to a human readable string: ...
1
vote
0answers
33 views

Testing after_save hooks in rails 4.2 with MiniTest

I've recently started working on a new application in rails 4.2.0-beta2. I've been using this as an opportunity to learn more about MiniTest, with a stretch goal of being as strict with myself with ...
1
vote
1answer
26 views

DRY up ivar assignment in Rails new and create controller actions

When creating a new model in Rails, I need to pass a bunch of other models into the view so I can define the correct associations. All I’m really doing here is querying the persistence layer for ...
6
votes
2answers
61 views

How to avoid mounting SQL by concatenating strings?

I found myself mounting a query by concatenating strings. This just seems wrong. Any ideas of how to avoid this kind of situation? I'm using Rails 4, and I don't see how can ...
3
votes
2answers
31 views

Initialise simple_form object inline

Using Rails 4 and a simple form, I'm rendering a couple of forms outside of their native controllers. Rather than scattering initialisers through my controllers, I wondered if it is cleaner to do the ...
0
votes
2answers
58 views

Make multiple conditions in if expression more readable

My logic has to be written in this way, but I think the if expression is too long to read. How can I make it more readable? ...
1
vote
1answer
42 views

View for displaying nounal and verbal pages

I've got this code in my View that just list two type of links. Here's an example output: Nounal Pages cleaners/house-cleaners Verbal Pages cleaning/house-cleaning Here's the view code. How ...
3
votes
1answer
52 views

Searching for something at a location

I'm using Searchkick in a situation where a user might write the location they are searching for, or the thing they are searching for, or both. I've been working on the below code for most of the ...
2
votes
1answer
66 views

Ruby on Rails Method ActiveRecord Query Optimizations

So I have three methods and I'm not sure how to optimize them. I will be working on this and updating this post while I do but basically running these three methods is central to some parts of my ...
0
votes
0answers
42 views

Rails Controller vs Service Object for Application Logic

I have a #change_account action in the accounts controller that verifies if the user has access to the requested account prior to changing the current_account which happens via setting the ...
2
votes
0answers
71 views

DRY my rake import from external database tasks

The application I'm working on runs import tasks each night to get information from an external database and processes and writes it to the local PostgreSQL database. This is so it can be viewed in ...
0
votes
1answer
14 views

Model with many associations produces code complexity

I've refactored this model about 50 times according to codeclimate, trying to get the codesmell down. With all of the associations, I feel like this is as bare bones as I can get it while still ...
5
votes
2answers
97 views

Rails controller method that conditionally filters results

This code for my blog checks to see if tags are in the params hash. If they are, then only posts that are tagged will be paginated. Otherwise, all of the posts are paginated. ...
4
votes
1answer
41 views

Calculation in Concern module using Rails

I wrote the following Concern module. Is it good practice to perform all the calculations in the included scope? How can it be ...
2
votes
1answer
28 views

How should I tell Rails which of these content types to load for a given feed?

I have content management system for an iOS app built with rails. Right now there a are three different types of content. The first two are just strings uploaded into a textfield by the user. One is ...
1
vote
1answer
31 views

Showing a table for open and closed tickets

I have written the following code. It shows a table for open and closed tickets. The code for open tickets is identical with the closed tickets. I was trying to figure out a way to just write one code ...
1
vote
5answers
59 views

Refactor auto-incrementing function in Ruby on Rails

I have a Rails app where we generate calls. Each call has an incident_number which is a unique number based off the following format: 14-00001 (year in 2 digit followed by a dash then a 5 digit ...
1
vote
1answer
39 views

Refactor Coffee Script to fire on page load instead of change

I have a Rails app where I'm trying to select a list of facilities based on what region is selected in a form. So far, I've implemented group_collection_select to do this as well as a bit of ...
1
vote
4answers
18 views

Issue in Ruby Programming Style

I am new to Ruby on Rails. Can some one please help me with this question? How would you modify the following to be consistent with Ruby's style? ...
2
votes
0answers
60 views

Rails app that scrapes forum using Nokogiri gem

I've built a website that scrapes a guitar forum's pages and populates Rails model. I'm using rake task along with heroku scheduler to run background scrapes every hour. On the homepage, the forum ads ...
3
votes
0answers
85 views

Heterogenous tree in the application domain: How do I represent them?

The Domain I have three types of items in my domain: ItemA, ItemB, ItemC. (I can't use their real names.) ItemA has one attribute: thing_id. ItemB has 6 attributes: thing_id, name, description, ...
2
votes
1answer
50 views

Refactor Rails Controller and View to generate less SQL queries

I have a Rails 3.2.14 app where I have a home controller (dashboard). In this controller and view I'm calling multiple instance variables to get different counts based off of scopes I've created in ...
2
votes
1answer
72 views

Bitwise operation to get the weekday name

The function days_name receive a val (string) corresponding to weekdays in hexadecimal. ...
3
votes
2answers
66 views

Rails Carousel View

In my web site, I created a carousel. Just now, I realized that I most likely repeated myself with this code. I would like to learn how to DRY up this code. Here is my view file (.html.erb) format: ...
3
votes
1answer
71 views

Check if a record already exists and update if it exists

I want to check if a record already exists in a database, and return true or false. If it exists, I want to update the ...
5
votes
1answer
82 views

A conditional active record query

I have a conditional active record query that checks to see if a relationship of type X exists (.count > 0) where attribute x ...
1
vote
2answers
39 views

Refactoring complex phone validations

I have a complex phone validation that does the following: First, check if either a home phone, mobile phone or work phone is provided (there will be 3 text fields in form and at least one type of ...
5
votes
2answers
89 views

DRYING up multiple controller action pages in Rails code

One of my rails controllers is horribly overcrowded with a bunch of methods that link to static web pages. Controller ...
2
votes
1answer
77 views

Graceful way to iterate through two arrays and build a master array?

params[:controller_id].each do |cid| params[:zone_number].each do |pzn| zone_ids << Zone.find_by_controller_id_and_number(cid,pzn).id end end I have ...
1
vote
1answer
32 views

Checking if an associated object has failed to save/create

I smell something wrong here, but am not quite sure what the most idiomatic way to solve the problem. I have a controller for (job)listings and it has a method ...
5
votes
3answers
725 views

Repetitive controller with all methods nearly identical

I have an API with the following code ...
4
votes
1answer
80 views

Better way of checking if the current user already like a post

I have three models which look like this: ...
4
votes
1answer
96 views

How could this “complex method” for codeclimate be refactored?

I'm trying to figure out how to refactor some code based on what codeclimate is telling me. So far it seems that any method that is longer than 5 lines or has an ...
1
vote
0answers
69 views

Variable assign expression with multiple expressions

I have three types of variable assign expression with multiple expressions. It looks ugly, but I'm not sure how to make it more readable. Type 1 ...
1
vote
0answers
89 views

Written worker using sidekiq for activerecord class

I have two model classes as follows: ...
1
vote
1answer
54 views

Better way of comparing integers

I wrote this class which is comparing some values, how can I improve its performance and maybe make it prettier? You can see at the bottom what type of data it requires, also the array size can reach ...
1
vote
0answers
12 views

Memcache Keys Management Class

I have written below class to manage Memcache keys. My Memcache keys structure are like: ...
0
votes
0answers
28 views

Rewrite Active Record query

How can I improve the readability of the below query, and maybe improve the performance as well? ...
3
votes
3answers
154 views

How would you refactor this case statement?

I've got this code in my ApplicationHelper file: ...
0
votes
1answer
42 views

Store location with some excluded path

This code stores the last know location in the session. The location is not stored if it is one of the exceptions in the if statement. ...
1
vote
1answer
58 views

Cleaning up Rails User Model

this is an app that I started on a while back to get digging into rails. It's grown and is now a functioning app. However, the user model is now over 200 lines long. Would love your opinion on how to ...
5
votes
3answers
132 views

Search for User by first and/or last name, efficiently

I am refactoring a user search that just feels dirty. (Users#search) I need to allow a blank param to search on partial or only a first or last, but don't want to return all the records in the ...
1
vote
1answer
110 views

Various RSpec tests

I've just written my first full batch of RSpec tests and wondered if anyone can comment at a glance on how they might be written easier, more readable, faster etc. There are some pending specs so ...
1
vote
1answer
56 views

Is my code clean enough and standard when processing Ruby on Rails requests using User's time zone?

I have an application in Ruby on Rails (3.2) and I want all the requests to take into account the currently logged in user's time zone. Also, I want to have a helper that will let me display date ...
1
vote
1answer
39 views

Move update_attributes into class and check for non-nil params to update only

There are a number of things wrong with the code here: ...
5
votes
2answers
54 views

Refactor .each where each is redirected into a hash

What would be the best way to refactor this : ...
2
votes
2answers
36 views

Refactor mass assigning from a hash

Here is the piece of my controller action that takes a session[:auth] hash and then assigns the values to the ActiveRecord object represented by ...