ruby-on-rails

Can Rails models have too many associations?

Apologies if this question is a bit daft, but are there any negative repercussions if an app has a model or models with numerous associations? For a complex app requiring a User model, for example (eg. a social networking site), it's plausible that the model could have 15+ associations (has_many :posts, has_many :messages, has_many :phot...

Subclassing ActiveRecord with permalink_fu in a rails engine

This question is related to extending class methods in Ruby, perhaps more specifically in the way that permalink_fu does so. It appears that has_permalink on a model will not be available in a derived model. Certainly I would expect anything defined in a class to be inherited by its derived classes. class MyScope::MyClass < ActiveRecor...

Is it possible to have all of a controller's actions go into the action cache without specifying each action to caches_action?

I've got a controller for semi-static pages (the only content that changes is a login/logout link and the presence/absence of a link to the user's home), and I'd like to put the static content into the action cache for every action. Is there a way to specify to cache all of the pages, without having to manually add each page? I.e., i...

How do I use Fixtures.identify?

What are the parameters and what is the significance of them? ...

Can Ruby on Rails's respond_to return a line when the format is not supported?

A usual usage of respond_to is like respond_to do |format| format.html format.xml { render :xml => @data } end can it be made so that when the format is not supported (such as json or csv not being supported above), instead of returning nothing, return a text line saying "the format is not supported", or better yet, have it automa...

Ruby on Rails and WebSphere Can I have both and either?

I read where you can have Ruby on Rails running in WebSphere and I think I might try it. If I make the website in RoR in WebSphere but later want to run just ruby on rails with with Apache, can I do that? I want websphere because I'll want it for other things but I have another problem where the same website will suffice but cannot tak...

Automatically grouping images into albums

We're building a piece of forum software for a client. On posts, they want users to be able to upload images (we're using paperclip right now) to insert into the body of the post (similar to wordpress). But they want the system to be able to detect if a number of photos are grouped together, and if so show them in a gallery view (thumbna...

[ActiveRecord 3] how can I make an 'OR' statement

Hey, I am trying to create an 'OR' sql statement in ActiveRecord 3, I tried all kinds of variations but cant figure it out... I am new to it so I might be missing something obvious. For example I want this query to include multiple 'channel_ids' and have it return all posts for any of the channel IDs. This works for one: Post.where(...

When to call a "require" in Rails?

I have a more conceptual question in Rails... or Ruby for that matter: Is it best to call a require right before the method that needs it, group my requires at the beginning of the class or somewhere in an initializer when Rails boots? Does it matter from a performance point of view? From a readability point of view? Does it make a dif...

Rails 2.3 - Redirection Issues

I have an application where folks search for an item. If the item is found, they are presented with a list of related items found by parameters in the URL AND a contact form. I'm having issues redirecting the visitor back to the same page (with the URL parameters) after submitting the form. Any ideas? ...

Ruby on Rails: how do I get the top 5 of something?

I have a column, money, and I want the top 5 most expensive activeRecord items. How do I do that? ...

Ruby on Rails: how do I sort with two columns using ActiveRecord?

I want to sort by two columns, one is a DateTime (updated_at), and the other is a Decimal (Price) I would like to be able to sort first by updated_at, then, if multiple items occur on the same day, sort by Price. Ideas? ...

Overridable Active Record Fields

I have a cms and public facing website built in rails that pulls in an XML file (from another system) every hour that contains all our product data. But once in a while, the marketing team might want to override certain fields in order to display them differently on the website. For example, they may want to alter the name of a product...

How can I stop action_cache from unescaping my ampersands?

I'm generating html and js to send to my page via AJAX. That part works great - everything renders fine, and my ampersands are escaped. When I user action_cache, the data in the cache file has un-escaped ampersands in the textual portions of my html (links remain escaped). So, it breaks when it tries to update the page. Why does the c...

How can I force Rails to synchronize transactions to a MySQL table across connections?

I have a high-traffic site that records web hits in a table, Hits. There's logic in place so that duplicate hits are discarded (where the definition of what defines a hit as duplicate is arbitrary for the purposes of this example). Problem: with multiple web servers running (against the same DB), two or more hits can arrive at the same ...

Inherit active_record in rails.

Right now my classes are look like this. class BalanceName < ActiveRecord def before_validation set_blank_attributes_to_nil(@attributes) end end class Balance < ActiveRecord def before_validation set_blank_attributes_to_nil(@attributes) end end I want to inherite activer record into one class and than want...

Rails 2.3 - Render Issue on failed validation (one controller to another)

I have a controller (items) that renders a contact form if an item is searched for and found. The items are find by url parameters. The problem I'm having is that if validation fails, I don't know how to re-render the contact form back into the items controller while maintaining the url parameters. Thanks in advance for any help. ...

ruby rails functional test problem

I'm getting the following problem with my functional test. mrbernz:mylife bernardleung$ ruby test/functional/forums_controller_test.rb ..... 1) Error: test_should_create_forum(ForumsControllerTest): ActiveRecord::StatementInvalid: Mysql::BadFieldError: Unknown column 'id:3 name' in 'field list': INSERT INTO roles (id, id:3 name) VALU...

Make all ActionMailer deliveries use delayed_job by default

Is there a way to make all ActionMailer deliveries enqueue a delayed job by default, instead of doing it synchronously? ...

Group Query with Calculations on Rails 3

Rails 3 problem. I have a Foods table of foods with the following attributes: name calories (per gram) fat (per gram) carbs (per gram) protein (per gram) I then have a LoggedFoods table representing a food that has been eaten at a given time. It has the following attributes: food_id number_of_grams_eaten ate_when (datetime) So th...