Tagged Questions
0
votes
0answers
42 views
New library for declaring units on numerical model attributes
A couple of days ago as I was working on a project I stumbled something I consider a problem and that, as far I know, doesn't have any good solution available in Rails.
The problem is attribute ...
1
vote
0answers
14 views
Dynamic Controller Creation in Rails
I have overrode Rails' ActionDispatch::Routing::RouteSet::Dispatcher.controller_reference method to check if a controller exists by checking for the required constants and then creating them based ...
2
votes
1answer
48 views
How do I refactor this ActiveRecord object to lessen dependency on callbacks?
I have an Order object that belongs_to a BillingAddress and a ShippingAddress. I want to present my user with only ShippingAddress fields and a checked checkbox indicating that the billing address ...
2
votes
1answer
60 views
Mapping arrays to ranges in Ruby
I have a simple Rails app, which is used to run some clinical surveys. Participants answer sets of questions (multiple-choice, valued 1-5), and, within each set, the answers are summed up and the ...
0
votes
1answer
63 views
Math Calculus in ruby
I have several calculus to do in my ruby app.
My code is currently working but I find it very ugly.
@guestreviews = GuestReview.where(:reviewed_id => @user.id)
@hostreviews = ...
1
vote
1answer
57 views
Rails: Setting a transient attribute on a set of objects from one model based on information from a junction model
I have a user model, a task model, and a junction model user_task that saves the data joining the two:
class Task < ActiveRecord::Base
attr_accessor :completed_on
has_many :user_tasks
class ...
3
votes
1answer
133 views
Nasty Age Printing Method
I have this ugly age printing method. Can you do better?
def age(birth_date)
today = Date.today
years = today.year - birth_date.year
months = today.month - birth_date.month
(months -1) if ...
1
vote
4answers
71 views
can this rails code be simplified and be more efficient? nested if else statements
Here's a part of my controller and it's getting quite lengthy (the code works).
Would this code slow down the performance of my website? Can it be cleaned up and be written more efficiently?
def ...
2
votes
3answers
72 views
I need help refactoring some Rails code that looks clunky.
I would like to refactor this block, it looks clunky:
# refactor me
receive_payment_on = false
config[:sections].each do |section|
if section[:applicants]
section[:applicants][:sections].each ...
2
votes
1answer
54 views
two or more render/redirect in the same method
I often have method like this one with two or more render to do due to catching the error for example.
I currently do something like this:
def update
@user = current_user
if ...
3
votes
2answers
106 views
How would one more elegantly parse data from XML using Ruby and Nokogiri?
I have a method that parses XML into an array of hashes.
Here is the original XML:
<rowset name="skillqueue" key="queuePosition" ...
2
votes
1answer
47 views
Rails helper method refactor
I have this messy helper method:
def gesture(klass, item, text, desc)
element_class = klass.to_s + " gesture"
content_tag :li do
if klass == :sell
link_to new_reply_path(item_id: ...
0
votes
1answer
33 views
What is the best way to format a large has_many through line?
This question is about code style.
I have this line in one of my models:
has_many :owning_artists, :through => :artist_tracks, :source => :artist, :conditions => { :artist_tracks => { ...
2
votes
1answer
44 views
Refactor Ruby ActiveRecord importing library
In my Rails app I need to import some files from CSV and Excel files. I needed it in 2 models so I have written lib/importable.rb:
module Importable
def self.included(base)
base.send :extend, ...
1
vote
1answer
89 views
How can I make this code DRY and KISS in my model?
class Rating < ActiveRecord::Base
attr_accessible :item_type, :item_id, :rating, :voters_up, :voters_down
serialize :voters_up, Hash
serialize :voters_down, Hash
belongs_to :ranks, ...