ruby-on-rails

Rails: Non id foreign key lookup ActiveRecord

I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes, this rightly triggers SELE...

Sending Email via ActionMailer::Base using SMTP, but sender should be another email-adress

Hello, i'm searching for a way to send emails with ruby on rails via actionMailer. The difficulty is, that i am sending the emails via gmail smtp. But i have to send them from different email-adresses (the customers have to be able to setup there own email adress, but i dont want to save the credentials). Any Idea? Thanks"! ...

Parsing custom feed elements using FeedZirra

Is there a way to parse feed's custom elements? Not feed entries', feed's custom elements. I know there is a way to do the same for the entries. Like, Feedzirra::Feed.add_common_feed_entry_element("wfw:commentRss", :as => :comment_rss) feed = Feedzirra::Feed.parse(some_atom_xml) feed.entries.first.comment_rss # => wfw:commentRss is now ...

Adding collection routes to nested resources

I need to add some collection routes for a nested resource. I have a subscription controller and I need two new methods to add here. change_plan and update_plan Basically I need the urls looks like; http://localhost:3007/admin/accounts/1/subscriptions/7/change_plan http://localhost:3007/admin/accounts/1/subscriptions/7/update_plan So...

Making fullcalendar scroll to the current time?

Simply put, is there a way to have fullcalendar scroll to the current time position in the week view? If it helps, I'm using Ruby on Rails and jQuery ...

Question about ActiveRecord#default_scope method and default ordering

Question about default_scope with Rails 2/3. On my Rails 3 project, I'm using a lot default_scope to order by created_at desc. So first I wrote : default_scope order("created_at desc") in many of my models. But the problem is that created_at exist in almost every of my application tables... So if I write a query that simply makes a j...

Querying a polymorphic association

I have a polymorphic association like this - class Image < ActiveRecord::Base has_one :approval, :as => :approvable end class Page < ActiveRecord::Base has_one :approval, :as => :approvable end class Site < ActiveRecord::Base has_one :approval, :as => :approvable end class Approval < ActiveRecord::Base belongs_to :approvable...

How to strip a quote in a Cucumber Then-Step?

Hello everyone, i want to strip a quote in the following cucumber expression, but i get unknown step error Then I should see "[\"20257\"]\n" Cucumber suggested me this :( Then /^I should see "([^"]*)"(\d+)\\"([^"]*)"$/ do |arg1, arg2, arg3| pending # express the regexp above with the code you wish you had end Any ideas, how to s...

Refactoring SQL

I must connect from my Rails app to a remote database on a Java application. I have a query like this: find_by_sql("select c_templateid, c_templateinfoid, c_startdate, c_enddate, c_active, campaign, shortcode, prefix, c_descriptivename, c_description, c_templatename from (active_services aser join activity a on aser.c_primaryprefixid ...

Using searchlogic in Rails to find records by created_at year

I'm working on a Rails app wherein I want to be able to search for records created in a given year using the Searchlogic gem, but I can't figure out how to have Searchlogic only search by year. I tried this in my search form: <%= f.date_select(:created_at_equals, :start_year => 2010, :end_year => 2015, :discard_day => true, :discard_mo...

@controller is nil when testing delete :destroy with Shoulda

The code below: context "should destroy participation" do setup do @p = Factory.create :participation delete :destroy, :id => @p.id.to_param end should_redirect_to(:controller => 'configuration', :action => 'edit') end Gives me the error below, any idea why? RuntimeError: @controller is nil: make sure ...

Adding times in ruby on rails

Hello, I currently have a model called Job which has an attribute called CPU. This corresponds to the CPU time that a job was running. I would like to add all the time attributes of all the jobs for a specific date. This column is in the time format 00:00:00. Therefore I thought this would work: def self.cpu_time sum(:cpu) end Which...

Reuse models in multiple Rails applications

I have built a web application. This application is very specialized for just one task. In a new project, I'd like to reuse some database, model and controller I developed for the previous application. I don't need al the logic just some section. I don't want to cut and past pieces of models and controllers. Which is the best approach/...

Missing templates from new Rails 3 app?

Just tried writing a simple validates_presence_of in my model, and when the errors try to render, it calls this : Template is missing Missing template posts/create with {:locale=>[:en, :en], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml, :haml], :formats=>[:html]} in view paths "/Users/johnsmith/Sites/shwagr/app/views" Errors don't...

Updating a large record set in Rails

Hello all, I need to update a single field across a large set of records. Normally, I would just run a quick SQL update statement from the console and be done with it, but this is a utility that end users need to be able to run in this app. So, here's my code: users = User.find(:all, :select => 'id, flag') users.each do |u| u.flag =...

Date and Time in ROR

In my app I want the time/date to display as Month/Year (e.g. 7/10). The problem is sometimes I get class Date and sometimes class Time so I wind up with the following code in the application controller ... class Date def as_month_and_year self.strftime("%m").to_i.to_s + self.strftime("/%y") end end class Time def as_month_an...

Using a dynamic precision value in number_to_currency based on the decimal value

Thoughout our app we use number_to_currency(value, :precision => 2). However, we now have a requirement whereby the value may need displaying to three or more decimal places, e.g. 0.01 => "0.01" 10 => "10.00" 0.005 => "0.005" In our current implementation, the third example renders as: 0.005 => "0.01" What's the best approach ...

Memory Leak in Ruby net/ldap Module

As part of my Rails application, I've written a little importer that sucks in data from our LDAP system and crams it into a User table. Unfortunately, the LDAP-related code leaks huge amounts of memory while iterating over our 32K users, and I haven't been able to figure out how to fix the issue. The problem seems to be related to the ...

Insert rows on specific line in a file.

I have opened my existing file in r+ mode. open("#{RAILS_ROOT}/locale/app.pot", 'r+') do |f| end I want to insert some other rows at specific line no.. Like i want insert "Hii " on line number 10. "hello " on line number 2. "world " on line number 20. How may i handle it in ruby ?? ...

Ruby on Rails Sometimes Nested Resource

In a Ruby on Rails app I'm working on (2.3.8), I have a model User that can have Workouts class Workout < ActiveRecord::Base belongs_to :user end I also have Trainer->Client relationships (all tied to the User model through a join table) A user can add workouts for himself, BUT a trainer can also add workouts for his clients. I'v...