ruby-on-rails

Rails: Validating existence of an association

I have a Category and a Post model, with each Post belonging to a Category. Before creating or updating a post, I need to check that the category selected exists. What's the best way to validate this information? At the moment, I'm doing a find in the controller to ensure that the category exists. Is it possible to put these kinds of...

uninitialized constant MysqlCompat::MysqlRes (using mms2r gem)

Hi, moved a rails app of mine onto a new server and had to install a few gem dependencies. However, after installing the mysql gem I get the error, uninitialized constant MysqlCompat::MysqlRes, whenever I try to run a rake command that involves the mysql database. It seems I only get this error when I require the mms2r gem. Has anyone ...

ruby and ruby on rails book (2009)

What are the best Ruby / Rails book available right now ? Note : I know that this question has probably been asked before. But things change so much in the Ruby / Rails world that some of the books are not up to date or even teaches things that are deprecated in the newer release of Ruby / Rails. ...

Default size of text column in Rails (PostgreSQL)

If I have this in a migration: t.text :body How much text can I fit in :body? (I'm using PostgreSQL if that's relevant) ...

Joins across more than two tables with activerecord

A while ago I asked (Joins across multiple tables with ActiveRecord with named scopes) http://stackoverflow.com/questions/1189086/joins-across-multiple-tables-with-activerecord-with-named-scopes Now I need to create a named scope that involved joining across more than two tables, eg: named_scope :baz_category, lambda {|c| {:joins=>([:f...

How to render custom field with to_xml?

Hi, Everyone I am working on a custom accessor method like an example below: class Forest < ActiveRecord : Base has_many :trees def total_water_usage # summarize each tree's water_usage in this forest. # return as string end end class Tree < ActiveRecord : Base belongs_to :forest end That is, I need your help for...

Paperclip, Rails 2.3.3 and OSX

Hi, I have code in view <% uberform_for :profile, :html => { :multipart => true, :method => :put }, :url => update_avatar_path do |f| %> <%= f.file_field :avatar %> <p><%= f.submit 'Upload avatar' %></p> <% end %> in controller def update_avatar current_user.profile.update_attribute(:avatar, params[:avatar]) redirect_...

Creating a POST with url elisp package in emacs: utf-8 problem

Hello everyone. I'm currently creating a Rest client for making blog posts much in the spirit of pastie.el. The main objective is for me to write a textile in emacs and make a post to a Rails application that will create it. It is working fine until I type anything in either spanish or japanese, then I get a 500 error. pastie.el has this...

How do you convince your manager that your project needs a huge refactoring?

I have joined a rails project as a contractor. The project has been going for more than a year. The code is written by about 10 different developers and most of them are contractors as well. They have different code style. Some of them came from Java. The code has horrible scores with metric_fu. Many functions are very long (100 - 300 li...

Simulating an XHR GET request

In my RSpec tests, I need to simulate an AJAX GET request to the index action, and have been using the code as described in both the Rails docs and the RSpec book: xhr :get, :index This always fails though, as the test tries to load the show action (without any parameters) rather than the specified index action. The controller action...

Using attributes in Model.calculate method?

OK So I've got my models like so: Team has_many :users, :through => memberships User has_one :user_status Now in my Team model I want to be able to find the maximum 'updated_at' value of my 'user_status' models. I tried doing something like: Team has_many :users, :through => :memberships has_many :user_statuses, :through =>...

Overloading ActiveSupport's default to_sentence behaviour

ActiveSupport offers the nice method to_sentence. Thus, require 'active_support' [1,2,3].to_sentence # gives "1, 2, and 3" [1,2,3].to_sentence(:last_word_connector => ' and ') # gives "1, 2 and 3" it's good that you can change the last word connector, because I prefer not to have the extra comma. but it takes so much extra text: 44 ...

Use Rails Migrations to delete an index without knowing its name

I have a table with a compound index that wasn't created through a rails migration. Now, I need to create a rails migration that will delete this index and create a new one, but I don't necessarily know what the name of the index will be. I know that it is possible to get a list of table names and column names within a migration step. I...

How does one avoid a large number (between 20 and 30) of embedded "if" statements in Ruby?

I've got a massive script with about 20 embedded if statements (Yay!) that is used to parse through a data file. And in a sense, that's correct because the script should not continue operating if any of those evaluations fail. But my gut says there's a more elegant way to accomplish the same thing. I'm familiar with the statemachine plu...

Javascript in a Rails remote_form_for partial

Hi, I have a contact form on my site that operates with a rails remote_form_for. After someone submits the form, the html in that div gets replaced with a success message instead of the entire page reloading (the reasons for doing this aren't too relevant...but basically this contact form is way below the fold and I don't want the page...

assign/replace params hash in rails

I have the code sequence below in a rails controller action. Before the IF, params contains the request parameters, as expected. After it, params is nil: can anyone please explain what is happening here? if false params = {:user => {:name => "user", :comment => 'comment'}} end Thank you. ...

rails auto_complete plugin. how do i pass authenticity token?

I tried the auto_complete text field in rails 2.3.3 and the server says it denied request because of no authenticity token. I can see that the helper doesn't automatically create a parameter for it. How I can manually do this? And I do not want to disable forgery prevention for this autocomplete. ...

Ruby on Rails sortable list

I created a sortable list in my RoR project, unfortunately it's not saving the list position. Upon page refresh the items return to their normal spot. I've pasted the code below or you can git it: git://github.com/mdgrech/23notes-.git app/views/notes/index.html.erb ////////////////////////////////////////////////////////////////////////...

why is my first RoR app on Dreamhost running so slow?

I've got my first RoR app deployed to Dreamhost and it's using Passenger. The one note on Dreamhost's wiki about slow response mentioned changing a RewriteRules line in the public/.htaccess file to use FastCGI. But I assume this will have no effect if I'm using Passenger, is that right? I've looked at the logs and compared them to my lo...

How should I run delayed_job in production environment?

I have no problems with running it in development mode via rake jobs:work. However, I'm somehow unable to figure out how to use it in production. I'm using Capistrano for deployment. Thanks for any advice! ...