ruby-on-rails

Using collection_select with an array

I am trying to use a dropdown in my edit.html.erb that is populated with a list of icon filenames (on the server). I used an array of label/value pairs to populate the list, which works great, except I want the dropdown to be preselected with the value that is currently in the record. When using collection_select with an actual model, th...

Rails: Initializing attributes that are dependent on one another

I have the following classes in my ActiveRecord model: def Property < ActiveRecord::Base # attribute: value_type (can hold values like :integer, :string) end def PropertyValue < ActiveRecord::Base belongs_to property # attribute: string_value # attribute: integer_value end A PropertyValue object is intended to hold only a str...

Share methods between named scopes

I have a bunch of named scopes and have a method within one of them that I would like to share between the other named scopes. I've sort of accomplished this by using define_method and a lambda. However, there is still some repeated code and I'm wondering is there a better approach? Here's a simplified example of what I've got. Assume I...

How to create a full Audit log in Rails for every table?

We recently began a compliance push at our company and are required to keep a full history of changes to our data which is currently managed in a Rails application. We've been given the OK to simply push something descriptive for every action to a log file, which is a fairly unobtrusive way to go. My inclination is to do something like...

Trying to do a CSV to multiple tables import with FasterCSV

I have a fasterCSV rake script but it only imports to 1 table. I am importing products and the products have several pieces of info stored on other tables. Current script: http://gist.github.com/321889 What I need to add is some way to import product info to another table at the same time. Say a category name. In this case: the ro...

Image upload options for website users

I want to be able to provide my website users with the ability to upload a profile picture. What are my options? What is the best way to do this? I would need to be able to limit the image size, crop / resize the image so that I can display thumbnails of the image. The website is written using Ruby on Rails ...

visual_effect after replace_html not working in rjs

Hi, In learning ruby on rails I've created a blog site. When the user adds a post via AJAX, the following rjs gets called: page.replace_html 'posts', :partial => @posts page.visual_effect :Highlight, 'post_' + @post.id.to_s However, the highlight isn't happening.. neither is any sort of effect even a hide. Clues: It works if I ju...

Rails ActiveRecord date between

I need to query comments made in one day. The field is part of the standard timestamps, is created_at. The selected date is coming from a date_select. How can I use ActiveRecord to do that? I need somthing like: "SELECT * FROM comments WHERE created_at BETWEEN '2010-02-03 00:00:00' AND '2010-02-03 23:59:59'" ...

URL getting truncated at 255 characters

I have a JavaScript widget which communicates with my Rails app by creating tags in the DOM. Every once in a while, I see a malformed request in my server logs, where the URL is truncated at 255 characters: http://myapplication.example/mycontroller/1/myaction?hostname=www.mycustomer.example&amp;request[param_a]=3&amp;request[param_b]=1...

Debugging breaks on Rails's "break" in ActiveSupport::Callbacks.run

Ruby-debug is getting hung up by breaking on the "break" ruby reserved word on line 94 activesupport-2.3.5/lib/active_support/callbacks.rb. def run(object, options = {}, &terminator) enumerator = options[:enumerator] || :each unless block_given? send(enumerator) { |callback| callback.call(object) } else send(e...

In Rails, how do you add multiple foreign keys to the same model? I have the Django equivalent.

I'm using Rails 3 Beta and I assume the syntax is similar to the 2.x. I'm also not very familiar with Ruby and Rails. In Django, multiple foreign keys to the same model looks like the following: class Dish(models.Model): name = models.CharField(max_length=100) lunch = models.ForeignKey(Ingredient, related_name='lunch') din...

How do I configure nginx to have a Rails app at a domain and WordPress at /blog/ ?

I've got a Rails app deployed via nginx/passenger. It will have multiple domains pointing to it. I'm wondering if it's possible to configure nginx so that any URL that matches [somedomain.com]/blog/ will be servered by PHP/WordPress located in a different directory. So, for example: domain1.com, domain2.com, & domain2.com/some-resou...

Ruby 1.8 vs 1.9 market share for Rails Apps

Anyone seen any tangible metrics or sites that talk about the percentages of Rails apps in production that are using Ruby 1.8 vs Ruby 1.9? In other words, we're contemplating back support for 1.8 but want to know if it's really worth it. Thanks! Chad ...

Is Cassandra production ready for Ruby on Rails?

I'm working on a project that is considering using Cassandra as a database. We would like to eventually migrate to Cassandra even if we use MySQL to start with, given its scalability. I know that big companies like Facebook, Digg, and recently Twitter is using Cassandra, but I don't believe any of those sites run off Rails. My questio...

Ruby-on-Rails: How to pull out most recent entries from a limited subset of a database table

Imagine something like a model User who has many Friends, each of who has many Comments, where I'm trying to display to the user the latest 100 comments by his friends. Is it possible to draw out the latest 100 in a single SQL query, or am I going to have to use Ruby application logic to parse a bigger list or make multiple queries? I ...

Safari caching pages too heavily, how to add expires header using rails

I'm page caching the majority of my pages and using a dynamic js file to inject the user specific content. So in my header file I have something like this: <%= javascript_include_tag '/dynamic_header/current' %> What this will do is execute the dynamic_header controller show.js.erb view which gets returned alongside the page and hand...

Adding to ActiveRecord::ConnectionAdapters::MysqlAdapter

For general knowledge and entertainment purposes I am trying to add some behavoiur to Rails. What I am looking for is simply to run a Mysql "EXPLAIN" statement before every select statement that Rails runs. I think this should work ok but I am getting the error: /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext...

Create new test suite with Rspec or extend current Test::Unit setup?

So I'm extending a friend's project and he's done all the development with TDD using Test::Unit I use Rspec in all my projects and want to avoid having to learn a new tool. Is it bad practice to have 2 separate test suites, one in Test::Unit and one in Rspec? I've also considered using Shoulda to extend Test::Unit to sort of feel like ...

Breaking apart some Ruby code taken from Rails

Would someone be able to break down the Ruby specifics of what each of these statements consist of in as far as methods, parameters, block interpretations etc. This is very common to see in Rails code and I'm trying to understand how the Ruby interpreter reads this code: respond_to do |format| format.xml { render :layout => false } en...

What's the best way to strip created_at and updated_at from an ActiveRecord instance?

Hi, I need to strip the created_at and updated_at fields from an instance before it's serialised and pushed to a client. What is the neatest way to achieve this? Thanks, Chris ...