ruby-on-rails

How can I set variables in Ruby with an if statement on one line?

With Ruby, how can I set a variable based on a condition? Something like: dog_name = params[:dog][:name] if params[:dog] And then dog_name just wouldn't be set if params[:dog] wasn't set. Right now I'm getting an error if params[:dog] is nil. ...

Database agnostic now() datetime function?

I've the following condition in one of my model which works for mysql. with_scope :find => { :conditions => "starts_at <= now() AND ends_at >= now()" } But I get error on the sqlite3 test database coz that now() is specific to mysql db only. What would be a db agnostic way for that now() function? ...

Is it possible to have file-system-like routes with acts_as_tree?

I have a Folder model which acts_as_tree. Is it possible that the routes represent the folder structure? Folders: 1: Folder A 2: Folder B 3: Folder C 4: Folder D 5: Folder E Routes: /folders/1 /folders/1/2 /folders/1/3/4 /folders/1/2/new /folders/... Is this possible? ...

Trying to find a count of items from a two level deep association, any ideas?

I am working on an application where I need to find the count of submitted items by users that have been referred by a user. For Example - User1 has referred 3 people (User2, User3, User4) and each of those users has submitted 5 articles. I am trying to find a way to get the count of submitted items in User1's tree (should be 15 in th...

RuntimeError: Declare either attr_protected or attr_accessible for User, but not both

Since a short time my rails applications yields the following runtime error in the test suite: RuntimeError: Declare either attr_protected or attr_accessible for User, but not both. This was probably introduced by an update to restful_authentication. But scanning the code for "attr_protected" only shows me it is never called. So why t...

MySQL Rails migration error: "Error on rename of schema_migration (errno: -1)"

Hi there, I'm a PHP dev and I'm new to Rails but have been getting on pretty well, everything seems pretty straightforward. However, up until this morning I have been using SQLite and decided to move what I'm building to MySQL. rake db:create works perfectly, but when I attempt to rake db:migrate I get the following error: rake abort...

Rails time zone selector: intelligently selecting a default

When signing up for an account on one of my apps, we need to store the time zone is in. We're using the time zone selector, which is fine, but I'd like to set the default value to something that it likely the user's current time zone. Is there an easy way, either on the server or using JavaScript, to set the time zone selector to the ti...

In Rails, how can one log the entirety of each incoming HTTP request?

I'm building an application that talks to a Rails backend that is POSTing requests to Rails. It's failing. Is there an easy way to have Rails log and spit to console the entirety of each incoming HTTP request? If anyone is feeling particularly charitable, you may be able to help with the underlying cause. Using a simple curl command w...

Versioning of Models in Ruby on Rails

I'm looking for a plugin/act to allow versioning of my models. It's kind of difficult to find a list of the available solutions. So far I gathered: acts_as_versioned simply_versioned vestal_versions The last two solutions only require a single version table - this sounds nice, but I've got a bad feeling about how the ease of migratio...

Question regarding the creation of an credit_card object on Ryan bates Active_Merchant intergration video

I was going through Ryan Bates' video on Active Merchant integration video Railscast #145 and my question was regarding the creation of the @credit_card method that he defines within the Order.rb method. def credit_card @credit_card||=ActiveMerchant::Billing::CreditCard.new( :type=>card_type, :number=>card_number, :verifi...

How to make Aptana (Eclipse) not follow symbolic links?

In Aptana Studio 2.0 (which is actually Eclipse 3.5) I have a Rails project X that includes a symbolic link to a folder Y outside the project folder. Folder Y is big. It contains a lot of files and folders. Building the project and refresing the workspace takes a lot of time. Therefore I do not like Eclipse to take the contents of folder...

A better way to conditionally display info in views?

I find myself frequently writing code like this in the HTML HEAD and other places: <% if @canonical_url %> <link rel="canonical" href="<%= @canonical_url %>"/> <% end %> I then set the variable in the controller if it's appropriate. Is there any way of writing the equivalent on one line, or maybe a different way of organizing the c...

Which popular rapid web development frameworks follow the "Rails paradigm"?

I'm trying to compile a list of notable web frameworks in wide use that follow the "Rails paradigm", which in the context of this question is defined as web frameworks following these key principles: Convention over Configuration (CoC) Don't Repeat Yourself (DRY) Sensible defaults Easy scaffolding: automatic generation of CRUD interfac...

What is process/reaper and why isn't it working?

When deploying my Rails app via Capistrano, the very last thing it tries to execute is this: sudo -p 'sudo password: ' -u app /home/user/public_html/example.com/current/script/process/reaper Then it throws this error: failed: "sh -c \"sudo -p 'sudo password: ' -u app /home/user/public_html/example.com/current/script/process/reaper\""...

Getting "Access denied for user 'root'@'localhost' (using password: NO)" in production mode and no errors getting logged

I deployed my Rails app and am getting 500 Errors on all pages. My production.log isn't showing anything (which is a problem), but I did a 'script/console production' and tried to run a simple query (User.find :first) and it throws this: Access denied for user 'root'@'localhost' (using password: NO) My database.yml file definitely has...

Advice/tools for working on a large existing rails application?

I recently joined a new company with a large existing codebase. Most of my experience has been in developing small-medium sized apps which I was involved with since the beginning. Does anyone have any useful tools or advice on how to begin working on a large app? Do I start at the models and move to controllers? Are there scripts to vi...

How can I improve a Ruby on Rails code that has a lot of SQL as strings?

I have a piece of Ruby on Rails code that has a complex SQL query (well, not that complex, but as far as I know beyond the ORM capabilities) and for my taste it has too many strings and harcoded values. I'd like to improve it as much as possible, so my question is open ended, what else can I do to improve it? Some particular issues I ha...

order that code is executed in, in an RJS script

I'm trying to implement a carousel function that's ajax driven. In other words, fetching new "slides" through the controller". I'm using glider.js (with prototype). My RJS file needs to load the new element into the DOM, and then Glider is called. My simplified RJS looks like this: page.insert_html :bottom, "content", :partial => 'pos...

Where to put common code found in multiple models?

I have two models that contain the same method: def foo # do something end Where should I put this? I know common code goes in the lib directory in a Rails app. But if I put it in a new class in lib called 'Foo', and I need to add its functionality to both of my ActiveRecord models, do I do that like this: class A < ActiveRecord:...

Please help me send a jpg file using send_data

I'm using the following tags in my html.erb to both display and download a jpg file that is not in the public/images folder: <%= image_tag retrieve_photo_path(@photo) %> <%= link_to "Download Photo", download_photo_path(@photo) %> my controller code looks like: def retrieve @photo = Photo.find(params[:id]) send_data File.read(@ph...