ruby-on-rails

Validate the quantity of associated objects

Given this: class User < ActiveRecord::Base has_many :photos MAX_PHOTOS = 5 validate :quantity_of_photos def quantity_of_photos ??? end end And this: @user.photos.size # => 5 I need this to fail: @user.photos << Photo.create(valid_photo_attributes) How do I do this validation? ...

Using will_paginate without :total_entries to improve a lengthy query

I have a current implementation of will_paginate that uses the paginate_by_sql method to build the collection to be paginated. We have a custom query for total_entries that's very complicated and puts a large load on our DB. Therefore we would like to cut total_entries from the pagination altogether. In other words, instead of the t...

Is there a better way to get this data?

Photographers "have_many" clients. Clients "have_many" events. Is there a better way to assign @events here if the user is a photographer? def index if @current_user.photographer? @events = [] @current_user.clients.each do |client| @events << client.events end else @events = @current_user.even...

Format DateTime in Text Box in Rails

Is there an easy way to format the display of a DateTime value in a Rails view? For example, if I'm doing something like: <%= text_field :my_object, :start_date %> and only want to display the Date part of the :start_date (i.e. I want to hide the time part), is there a way to format the :start_date string inside of the view such that...

Is it possible for a Rails/Django project to become a Death March?

I've worked on Death March projects in the Java world - projects that are destined to fail from the beginning because of some combination of poor management and unwieldy, complex technology, usually spanning multiple systems and often tied to a waterfall approach. Rails and Django are touted as Agile development technologies, which mean...

Selectively caching models with cache_money

Instead of doing this in the cache_money.rb initializer class ActiveRecord::Base is_cached :repository => $cache end I want to be able to selectively cache only certain models (the reason being our User model breaks memcached because it's generally too large to be serialized properly). class User < AR::Base is_cached :repository ...

jRails with autocomplete helpers

I've just started new branch of my project, where I try to use jRails. As far everything works awesome, except the autocomplete (especially text_field_with_auto_complete). What would be the best solution to make autocomplete working again? implement whole autocomplete by myself (I don't really like this one) rewrite the old prototype h...

ActiveRecord#first not returning the real first record

I am having an issue with ActiveRecord#first seemingly not returning the first record all of the time. >> Package.first => #<Package id: 22, name: "Test Package 4", created_at: "2009-09-11 21:10:54", updated_at: "2009-09-11 21:12:43", image_file_name: "0600-Sponsor.jpg", image_content_type: "image/jpeg", image_file_size: 29433> What I...

I want date select box with blank option default selected

I used the following date_select helpers but none of them worked as expected. I want the date select box with blank option default selected. With all the following code, I get the select box but with the current date selected. I'm on rails 2.3.2 <%= f.date_select :featured_at, :default => {:day => nil, :month => nil, :year => nil} %> <%...

Total Rails noob - Am I thinking right about the main page for my Web app?

I'm coming from the .NET world, and I'm trying to figure out the "Rails way" to design software. In ASP.NET, my usual instinct is to think in terms of pages. Very often I'll start with default.aspx (the main or index page) and build off that. But under Rails, that seems wrong. On the main page for my app, I want to show a list of ite...

How to validate that a string is a year

Using Ruby/RoR - The year is a string in the model/view. How do I validate that the user entered string is a valid gregorian calendar year? ...

How to Group by Day with Ruport / Ruby on Rails?

I'm trying to evaluate Ruport for use in my Rails app, but am not sure how to take a series of records with date/time stamps and group them via Ruport's grouping functions. I'm open to other/better methods to do this same grouping if Ruport doesn't make sense. ...

ActionView::MissingTemplate with HAML

I'm getting Ruby on Rails set up on a fresh installation of Snow Leopard. After battling (and beating) MySQL and Sphinx problems, I'm stuck on a stupid error related to HAML. Essentially I'm getting a missing template error for every view that uses HAML. I can add a blank xxx.html.erb file and and a (blank) page loads fine. But xxx.html...

Rails app stuck

Hi, I am running a rails app on Dreamhost. Today, a strange thing happened. A page is almost loaded (it seems to be fully loaded but the status is not 'Done') and after that, the app didn't respond on any page. I checked out the log and even the log was not complete. How do I know it? There are 3 missing images on the problem page and ...

Online users in Ruby on Rails

What is the simplest way how to check if user is online and display list of online users? The only way I can think of is some periodic polling server to update last action timestamp, and when last timestamp is more than xx ago, user is considered to be offline. But it doesn't seem like too eficient solution. ...

manage multiple reputation in database & code

Trying to implement a rating system of users and postings. What is the best schema for managing the multiple reputations. A user has a reputation derived from postings and feedback(similar to SO). Postings have a reputation too (again similar to SO). My initial idea as to have one reputation table. The rows would correspond to the...

What happened to my primary keys?

I'm using rails 2.3.4 When I execute rake db:test:prepare the id field created is not a primary key and auto-incremented. When I look at the development db no problem with the primary keys. Using MySQL. Edit: It looks like my schema.rb file was changed :id => false should be true. What generates this file? DB Migrations? [EDIT] What...

How to convert user input hours, minutes, seconds

In my model I have an attribute "duration" (seconds as an integer) But the views new/edit have input for: hours, minutes, seconds. Should I have in model (attr_accessor :hours, :minutes, :seconds) then how do I convert these virtual attributes into my "duration" attribute, Coming from Java I'd have setter/getter on "duration"! ...

Making URLs in email work with x_path

So the consensus seems to be that in order to get URLs working in the emails your application sends, you have to set config.action_mailer.default_url_options. This is well and good if you're using whatever_url or url_for, but what if there's some helper or partial that you want to use to generate an email that uses whatever_*path*, e.g....

erb: output repeats if template contains a method definition

I expected to see the word "test" appear in the output once and the word "hello" appear once. But I'm puzzling over the fact that if I do this, the word "test" is displayed twice. <div> <h3>test</h3> </div> <% def helo %> <% "hello" %> <% end %> <%= helo %> I assume there's a simple explanation for this related to some quirk o...