ruby-on-rails

Iterating over an ActiveRecord object's properties

Hi all, I'm trying to iterate over the properties of an ActiveRecord object. I've been searching online but I keep coming up blank. I thought I was getting warm with object.to_enum, but despite the fact that this returns an Enumerator object, I still get the error saying that 'each' is undefined for my object. Calling object.methods g...

convert named_scope for rails 3

I have the following code wich throws an error with Rails 3 : module Auditions module Search def self.included(base) base.extend ClassMethods ... base.named_scope :not_rated_by, lambda { |user_id| { :conditions => ['NOT EXISTS (SELECT * FROM audition_tags bar WHERE bar.user_id = ? AND bar.auditio...

Rails 3 Routing Error

I had this working yesterday, I don't know what I did to break it. Here are the routes for a RESTful controller: brand_responsibilities GET /brands/:brand_id/responsibilities(.:format) {:action=>"index", :controller=>"responsibilities"} brand_responsibilities POST /brands/:brand_id/responsibilities(.:format) ...

Rspec and Infinity Test in color

I am using infinity_test gem with rspec in a rails project. How can I get the output in color? I tried setting in terminal rspec --color I then ran "rspec spec/" to make sure color output was working. Then I run infinity_test and the test output is no longer in color. How can I get it to output in color? ...

Gem for oAuth2 Consumer AND Provider functionality in Rails 2.3.5

Hi there, I'm struggling finding a usable gem which provides the following for a Rails 2.3.5 application: we want to protect out API with oAuth 2; therefore a Controller for creating access/request Tokens and the models (Client, Token,...) are required we want to consume oAuth2 Services like Facebook and Twitter It would also be nic...

Singleton virtual model (Rails 3)

Hi, I would like to write a singleton virtual model (using Rails 3). Here is my code: class App extend ActiveModel::Naming include ActiveRecord::Associations include ActiveRecord::Reflection has_many :users def self.name 'MyApp' end end But that's not enough. If for instance I try this in Rails console: > App.firs...

Bulk update attribute on multiple models in ActiveRecord?

I have a simple has_many association, and I want to change an attribute from public to private the associated object. What's the best way to do this: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end user = User.first #=> #<User...> user.posts.count #=> 100 # something like t...

ActiveRecord - pre fetch single records from association

Consider the following models.. class Product < ActiveRecord::Base has_many :pricings end class Pricing < ActiveRecord::Base belongs_to :server end Pricing is a historical tracking table for prices of products, so there may potentially be hundreds of price points captured over time. What I want to add is a way to get only the...

Receiving error on Windows machine when Installing linecache ERROR: Failed to build gem native extension on

When attempting to perform a bundle install of an existing Ruby on Rails project I receive an error when the bundler reaches Installing linecache (0.43) with native extensions. I don't see linecache in the projects Gem file so I can't try bundling without it. I'm running Windows 7 and JetBrains RubyMine 2.0.2. I have tried bundling from ...

Rails decimal :scale is not working , what is the possible reason?

class CreateItems < ActiveRecord::Migration def self.up create_table :items do |t| t.string :name t.text :description t.decimal :value , :precision => 8, :scale => 2 , :default => 0 What is the possible reason of when I key in value in form ,value: 192.124325345 , I didn't get a 192.12 ? I rake db:rollback VERSION=0 before to ge...

Error using ActiveResource with Rails 3 and REST API

I have a REST API that I am trying to access using Rails 3.0.1 that returns: <Sessions> <Session> <Title>This is a sample title</Title> ...Misc </Session> <Session> <Title>Another Title</Title> ...Misc </Session> </Sessions> I have set up my session.rb class defined: class Session < ActiveResource::Base self...

how to change the value of Date.today within a running ruby process

Hi, I know this is a bad idea, but I have lots of legacy code and I want to run through some historical batch jobs. I dont want to change the systems date because other stuff runs on the same system. Is there any way that I can change the value that Date.today will return for the life of a given process only. The idea here is to rewind a...

Parse XML response from API, Javascript, Ajax

I have a rails app and i wanna to parse the xml response from API using Javascript (ajax). I tried: $(document).ready(function() { $.ajax({ type: "GET", url: "http://localhost:3000/users/mike.xml", dataType: "xml", success: parseXml }); }); function parseXml(xml) { ... } but don't ...

Images patch relative to [custom] view directory

Hi, In my Rails application, user may have custom page templates (not for the application) which are stored in public/user_templates/user1/ the view .erb file is also stored there in the user template folder. There is an images folder inside every template folder. How can I use the relative paths of the images in the .erb file? For e...

Wrong number of arguments for a helper

I'm trying to generate a form from a parsed json (in Hash form) with this helper def hash_to_form(hash, fields, legend) fields.fields_for do |b| concat('<fieldset><legend>', legend, '</legend>') hash.each do |key, attr| if hash[key].is_a? Hash hash_to_form(hash[key], b, key) else conca...

I need to find closest date in a rails app.

Hi, I need to find the closest date to a given date using a rails named scope. With MySql I could use DATEDIFF but I want to keep database agnosticism, following doesn't work with Sqlite: named_scope :closest_to, lambda { |*args| date = args.first count = args[1] || 1 {:limit => count, :order => "ABS(DATEDIFF(hour, date_field, #...

Correctly instantiating a nested fields_for without showing older saved objects ?

I have a nested form that instantiates as this : - 2.times { @organization.referrals.build } - form_for @organization do |f| = f.error_messages - f.fields_for :referrals do |f| Except, the nested forms are supposed to be always new and unique. Where as this form shows previously created objects as well. So I tried to write as so...

Rails - serialize another activerecord model

I'm looking to serialize an incomplete/temporary model as an attribute of another model such as: class User < ActiveRecord::Base serialize :pending_post end Where pending_post is assigned an activerecord model: ... user.pending_post = Post.new(:title => "Whatever", :message => "whatever") user.save But instead of saving the yaml ...

Ruby on Rails: using rails 2.3.x and 3.0 on same machine

I just finished a project with rails 2.3.8. Client won't pay for the migration, so I want to keep it as is. I want to start a new project, which is to build my own homepage. Want to use similar tools but want to go to rails 3.0. Can these versions co-exist on the same machine? If yes, what do I need to do? Found few pages online, but n...

Factory Girl: How to associate a record to another record without creating a new record?

Hello. I'm using Factory Girl/Rspec2/Rails 3. In factories.rb, I have: Factory.define :user do |user| user.name 'Some guy' user.email '[email protected]' user.password 'password' end Factory.define :org_admin, :parent => :user do |user| user.email '[email protected]' end Factory.define :user_with_memb...