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

1
vote
1answer
12 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
39 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
30 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 ...
4
votes
2answers
49 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 ...
1
vote
1answer
21 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
710 views

Repetitive controller with all methods nearly identical

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

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

I have three models which look like this: ...
4
votes
1answer
81 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
62 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
54 views

Written worker using sidekiq for activerecord class

I have two model classes as follows: ...
1
vote
1answer
51 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
27 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
136 views

How would you refactor this case statement?

I've got this code in my ApplicationHelper file: ...
0
votes
1answer
34 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
51 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 ...
4
votes
3answers
115 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
65 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
53 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
30 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
52 views

Refactor .each where each is redirected into a hash

What would be the best way to refactor this : ...
2
votes
2answers
32 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 ...
1
vote
0answers
14 views

Link in between parenthesis

A common pattern while creating links is adding a help link in between parentheses. Something like this: ...
2
votes
1answer
63 views

Ruby on Rails: refactor big search method

I have big search method in my model that corresponds to search a proper Car. It looks like this: ...
1
vote
0answers
28 views

Displaying blog posts from non-SSL site in SSL site

The Issue I added SSL to a previously static and non-SSL website because some new features will require it. I'm using rails 4.0.5, ruby 1.9.3, and the site is deployed on heroku. [it's at www dot ...
5
votes
1answer
62 views

Reverse-engineering with Filepicker API

I have this script to pull data out of the Filepicker API internal. It's mostly reverse-engineering and the code seems to be ugly to me. How can this be improved? ...
1
vote
0answers
49 views

scraping and saving using Arrays or Objects

I'm using Anemone to Spider a website, I am then using a set of rules specific to that website, to find certain parameters. I feel like it's simple enough, but any attempt I make to save the ...
1
vote
2answers
27 views
0
votes
3answers
110 views

Which syntax is preferred to create a Hash in Ruby on Rails?

I am writing some code in Ruby on Rails to Create an object. I am using Ruby 2.0 and Rails 4.0.3 in my Application. I have a Model called: ...
1
vote
1answer
90 views

Multiple search filter like the one in freshdesk in rails

I have requirement to apply multiple filters on database. So I am doing this now. I will have multiple conditions in the front end where the user can select the conditions he wants to filter with and ...
1
vote
1answer
53 views

Make an object belong to another object

What I'm doing: @cat = Cat.new(cat_params) @cat.user_id = current_user.id Is there a better way? Once you've created a cat you can do this: ...
1
vote
1answer
31 views

Implementation on AMQP in Ruby

I need comments on the below code: ...
0
votes
0answers
40 views

Creating a spec for confirming user accounts

I've created a spec that how a user confirms his account. It tests the following scenarios: While this code is completely untested (wrote it all in the browser) I don't see why it wouldn't work. ...
2
votes
1answer
101 views

Complex method - flagged by codeclimate

I am a fairly new (RubyonRails) developer. I desire to improve my coding skills so I used climate to do some review on my code. It gave me a flag for this method that its complex. Is it characterised ...
1
vote
1answer
75 views

Creating user accounts as an administrative user

I have a site where user accounts can be created in two ways: by new users (sign up) and by admin users. Since the creation is handled differently in each case, I decided to split the logic into two ...
2
votes
1answer
62 views

Rails preventing ajax view duplication

I have a website where people can write articles and publish/unpublish them. This is done via single button on the page: ...
5
votes
2answers
204 views

Concise nested statements

I really like the ruby ? : syntax for if else statements. However, I'm not sure how to do the following with a nested ...
2
votes
2answers
73 views

Transfer the format of an old string to a new string

First, I've extended the String class to detect uppercase and lowercase characters: ...
0
votes
1answer
44 views

Managing several payment means in my Rails 3.2 app

I'm trying to manage several means of payment in my Rails 3.2 application. Let's say I have 2 means : Credit Cards & Bank Transfers (BIC), working with 2 different payment providers. Those means ...
1
vote
0answers
54 views

DRY-ing the codes for controller specific javascript assets when using Turbolinks

I started to use Turbolinks gem in my Rails 3 application project. I use //= require_tree . in my application.js When using ...
2
votes
1answer
68 views

Is there a better approach to searching has_and_belongs_to_many relations?

I have Article and Category, with a has_and_belongs_to_many relation between the two. I ...
2
votes
2answers
128 views

Logging leads when interactions happen for a contact

I have been trying to get as close to SRP in my code as possible. I have a situation where I need to log leads when interactions happen for a contact. A lead may be created or updated. There are three ...
4
votes
0answers
84 views

Optimizing code for querying multiple related records in Rails

I have an Account, User and Group model. All User and ...
4
votes
2answers
139 views

Article voting schema

System I'm building has Users and Articles. Users can upvote/downvote Articles and cancel their votes if they want to. Each vote changes article's score. Score equals to sum of all vote values ...
3
votes
2answers
77 views

Rails refactoring has_many

I have a RoR 4 app, and its model have these associations: ...
3
votes
1answer
337 views

My first controller rspec test

I'm just started learning TDD with Rails and RSpec. This is my first test for controller. Just plain RESTful UserController that responds with JSON, so it has no new and edit methods. Please review it ...
4
votes
2answers
59 views

How can I DRY up these routes?

I have a controller with just a few actions, not the whole 7 RESTful actions situation. Here's what I have so far for routing: ...
3
votes
2answers
151 views

My first model RSpec

This is my first model RSpec test: ...
7
votes
1answer
145 views

ActiveRecord extension to find next / previous record(s) in one query

I just wrote this gem order_query to find next or previous records relative to the record assuming a (possibly complex) ORDER BY over the records. For example, this ...