ruby-on-rails

Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models?

Say I have a Rails Model called Thing. Thing has a url attribute that can optionally be set to a URL somewhere on the Internet. In view code, I need logic that does the following: <% if thing.url.blank? %> <%= link_to('Text', thing_path(thing)) %> <% else %> <%= link_to('Text', thing.url) %> <% end %> This conditional logic in the vie...

Rails RESTful controller and rendering after custom action.

How can I render after executing an action in a restful controller instead of redirecting. I have a controller with standard actions, and I added a special action that adds data to the resource in question, via a form on the #show page (Think comments on a post). I am validating the input from the form and want to re-render the show act...

How to handle UTF-8 email headers (like Subject:) using Ruby?

I'm an email n00b but I am working on an application that sends HTML email with Unicode characters (as my friend noted "enjoy encoding hell"). The Subject: header comes from user input and therefore may contain Unicode characters. Some mail clients (like GMail and Outlook 2007) are OK with this, but from my reading it seems the right wa...

Difference between "class << anObject" and anObject.class_eval

I see the following code in the attribute_fu plugin: module AttributeFu module Associations #:nodoc: def self.included(base) #:nodoc: ...

Ruby's MySQL driver not finding required libraries

Linux 2.6.18-92.el5, ruby 1.8.7, Rails 2.2.2, mysql gem 2.7 I installed the MySQL binary distribution under /usr/local, then installed the mysql gem like this: gem install mysql --no-rdoc --no-ri \ -- --with-mysql-include=/usr/local/mysql/include \ --with-mysql-lib=/usr/local/mysql/lib \ --with-mysql-config=/usr/local/mysql/bin/m...

Rails transactions

Trying to use ActiveRecord::Base.transaction I figured that rollback doesn't work by default using Rails 1.2.6 and mysql 5.0. Playing with it a little bit more I found out that autocommit is not set to 0 in mysql connection. Questions: 1) how do I disable autocommit in rails for all connections 2) will it have some negative impact on...

Does passing the params collection from controller to model break MVC?

I want to pass the params collection from the controller to the model to parse filtering and sorting conditions. Does having a method in the model that takes the params from the controller break MVC? ...

Rails Caching DB Queries and Best Practices

The DB load on my site is getting really high so it is time for me to cache common queries that are being called 1000s of times an hour where the results are not changing. So for instance on my city model I do the following: def self.fetch(id) Rails.cache.fetch("city_#{id}") { City.find(id) } end def after_save; Rails.c...

restful rails model validation

I am working on a rails application (I have some experience with rails). But, this time I am using RESTful to build it. I am wondering how do I validate my models in a RESTful fashion? What I mean by that is when a user enters data into a form, but the model validations prevent the model from being created what is a RESTful way to red...

UTF8 MySQL problems on Rails - encoding issues with utf8_general_ci

I have a staging Rails site up that's running on MySQL 5.0.32-Debian. On this particular site, all of my tables are using utf8 / utf8_general_ci encoding. Inside that database, I have some data that looks like so: mysql> select * from currency_types limit 1,10; +------+-----------------+---------+ | code | name | symbol | ...

What is the best way to generate a Rails app using edge?

Currently when I want to create a Rails application using edge I first just run... rails appname Then I... rake rails:freeze:edge Then I delete all of the folders but the vendor folder which contains the frozen edge. Once that's done I run (from the root of the site)... ruby vendor/rails/railties/bin/rails . I do it this way rig...

What are the various files that could have PATH declarations for OS X Terminal in them?

So I'm having a path issue on OS X Leopard. It seems OS X is adding other paths that I'm not stating and it's messing with my path priority. I only have a .bash_login file, I don't have a .bashrc or a .profile file. My .bash_login file is as such: export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH" When I run expor...

How do I reduce view duplication between client and server?

I'm working on an AJAXy project (Dojo and Rails, if the particulars matter). There are several places where the user should be able to sort, group, and filter results. There are also places where a user fills out a short form and the resulting item gets added to a list on the same page. The non-AJAXy implementation works fine -- the v...

Turning off authenticity token in Rails 2 for web services?

Instead of just filling out the form in HTML it should also be possible to just send a post request containing the params.. Is it possible to turn off the authenticity token if, for example, the Accept flag is set to 'application/JSON' in the HTTP header? ...

ruby on rails state machines

I'm looking to implement a state machine to manage a user moving through a series of steps over an extended period of time (weeks) with emails and then they interact with the app. I've looked at a couple of AASM plugins and forks (it seems like this plugin space has become a bit chaotic) and am curious what people would recommend. I sa...

Welcome/home page in Ruby on Rails - best practice

My homepage (or welcome page) will consist of data from two models (lets call them authors and posts). I am new to rails and not sure what is the best way to accomplish this. Should I create a new controller called welcome which gathers data from the authors and posts and then display them in the welcome index view? Or should I have a w...

best database strategy for a client-based website (Ruby on Rails)

I've built a nice website system that caters to the needs of a small niche market. I've been selling these websites over the last year by deploying copies of the software using Capistrano to my web server. It occurs to me that the only difference in these websites is the database, the CSS file, and a small set of images used for the ind...

How to return a Date (not a TimeWithZone) from a Oracle date column in ActiveRecord?

Environment: Rails 2.2.2, Oracle 10g Most of the columns declared "date" in my ActiveRecord models are exactly that: dates: they don't care about time at all. So with a model declared thus:# class MyDateOnlyModel < ActiveRecord::Migration def self.up create_table :my_date_only_model do |t| t.date :effective_date t.ti...

Friendly Byte Formatting in Rails

I need to format an integer representation of bytes into something friendly, and I'm hoping that there's a utility function in Ruby or in Rails that will do that formatting for me (to perpetuate my laziness, of course.) I'm looking for something that would look like: format_bytes(1024) -> "1 KB" format_bytes(102400) -> "1 MB" L...

What is the earliest callback one can hook in the Rails Framework?

I have found the before_dispatch and after_dispatch in dispatcher.rb but I need to access something earlier. Like around when Rails.public_path is defined. ...