ruby-on-rails

How do you extend class level methods to a separate module in Rails?

Given a situation such as: module Extension def self.included(recipient) recipient.extend(ModelClassMethods) end module ModelClassMethods def self.msg puts 'Hi from module' end end end class B include Extension end Why is B.msg not available? >> B.msg NoMethodError: undefined method `msg' for B:Class ...

Back again to use Rails 2.3.5 from Rails 3

Hi, Recently, I had seen information about Rails 3.0 beta and I had wanted to trial it. So I used gem update and installed this version. But now, I need to go back to Rails 2.3.5. How could I do it? I was thinking about this two solutions: Uninstalling Rails 3. I read that somebody removed Rails 3 by using gem uninstall but he cam...

jQuery with Rails when deployed to multiple subURIs

Hey. So I have a Rails app that's getting deployed to a production machine that serves it via Apache/Passenger to two subURIs. /app /app_2 both the above subURIs are running the same codebase. It's just two symlinks to the public dir that are both pointed to via Passenger by: RailsBaseURI "/app" RailsBaseURI "/app_2" Now, imma big ...

Error while using `find_or_create_by` on a `has_many` `through` association

I am running in to a problem while using find_or_create_by on a has_many through association. class Permission < ActiveRecord::Base belongs_to :user belongs_to :role end class Role < ActiveRecord::Base # DB columns: user_id, role_id has_many :permissions has_many :users, :through => :permissions end class User has_many :...

Rails: Can you pass arguments to the after_create method?

In my application, only users with the administrator role may create new users. In the new user form, I have a select box for each of the available roles that may be assigned to new users. I am hoping to use the after_create callback method to assign the role to the user. How can I access the selected value of the select box in the afte...

Is there any difference between these two 'belongs_to' statements

belongs_to :keeper, :class_name => "Staff" belongs_to "staff", :foreign_key => "keeper_id" In my basic tests, these seem to be doing the exact same thing. Are they indeed the same? Is one better than the other? ...

Heroku with caching

I'm using page caching within my application. Everything works fine locally. But after I push changes to heroku server. Caching expiration won't work. I use sweepers to track and expire cached pages. Here's config for caching in the production.rb config.cache_store = :memory_store #config.cache_store = :file_store, 'tmp/cache/' I trie...

Rails and Model Validation

I want to put some model level validation on the following table: create_table :audios do |t| t.integer :library_id, :null => false t.string :file, :null => false, :limit => 2048 t.string :name, :limit => 512 t.timestamps end Does this mean, that my model, which (so far) looks like: class Audio < ActiveRecord::Base belongs_...

Liquid Templates Not Parsing!

Im trying to use Liquid Template Language in my Rails Application, i've watched Ryan Bates' video over at rails cast, i pretty much follow the instructions but it just doesnt seem to work! When I try something like @template = Liquid::Template.parse("Hi {{name}}") @template.render('name' => 'toby') I get hi toby but when i try so...

Perform sum with nested controllers

I have two models of concern, "Order" and "Kit"; each order has_one :kit Each "Kit" has a 'cost' value. Within a controller I want to be able to sum together the costs for each 'order'. Logically I thought this would make sense (but it doesn't work): @revenue = Order.Kit.sum(:cost) Any help would be appreciated. Thanks. Example:...

How can I dynamically determine which model a controller is controlling?

I'm writing a Rails plugin, and need to be able to dynamically determine which model a controller is associated with. For example, if I have a PeopleController, I need a clean way to determine that the controller handles Person models. I've had a look through the API and haven't found a method for it. Is the only way to do this as a re...

Is there a shortcut in Textmate to wrap selection with multiline comment?=begin and =end

I want a shortcut in Textmate to wrap the selected text with a ruby multiline comment -- '=begin' and '=end'. It doesn't look like there is an existing one. Anyone know how to make one? ...

how to get next ID in a table in Rails

I have a table that has following columns id store_id store_name id and store_id are always going to be same. Since I am new to rails...I had created it by mistake. But I can't afford to go back and change stuff now since I am using store_id at a lot of places. Issue 1: I am making admin screen for this table. When I try to i...

Mechanize not recognizing anchor tags via CSS selector methods

(Hope this isn't a breach of etiquette: I posted this on RailsForum, but I haven't been getting much response from there recently.) Has anyone else had problems with Mechanize not recognizing anchor tags via CSS selectors? The HTML looks like this (snippet with white space removed for clarity): <td class='calendarCell' align='left'> <...

Integrating REST Web Services Provision with Main Application

I have a Rails web application that also exposes a few RESTful web services. The main application is secured and obviously uses its own security mechanism whereas the web services would be best authenticated with HTTP authentication. To be very DRY, the controllers behind the web services are shared by the controllers in the main applic...

Rails cache do inside module

Hot can I use cache do command inside module? Example module MyModule def self.some_method(str) cache str do ... some code ... end end end Thx! ...

What does string * '' mean in Ruby?

I was looking through some Rails source code and came across # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 129 129: def target! 130: @target * '' 131: end What does the * '' do? Is that multiplication by an empty string...? And why would you do that. ...

Workling worker cannot find newly added record

Hello, I'm using Starling and Workling to process background tasks in my application, a Swoopo-style auction site. In this case the background task is a notification system that monitors auctions and notifies the winner. The monitor is invoked upon creation of the auction object. My problem is that my monitoring code can't find the auct...

Accessing factory_girl factories in *other* factories

I'm using the factory_girl plugin in my rails application. For each model, I have a corresponding ruby file containing the factory data e.g. Factory.define :valid_thing, :class => Thing do |t| t.name 'Some valid thing' # t.user ??? end I have lots of different types of users (already defined in the user factory). If I try the foll...

Ruby on Rails, from an index action linking to individual item error

Hey guys, I'm working on a ruby on rails project currently, doing it without scaffolding or anything just by hand. I have an index controller that simply lists all existing items in the db with a link on each that redirects to a details view. The error I am getting is when trying to link to the item I have this line of code: <%= link_...