ruby-on-rails

Efficient accessor for new_record?s in ActiveRecord relationship?

Just getting through some of the issue backlog and could use some input on best-practices for what seems like it should be a common scenario. (Rails 2.3.5) Let's say we've got form for creating posts for a blog using mass-assignment. The create action looks something like: def create if @blog.update_attributes(params[:blog]) redi...

In Rails, any way to use app/helpers/foo.rb methods in Controller vs in View?

It seems that those helpers in app/helpers/foo.rb can be used in View, but cannot be used in Controller? In some cases, different controllers may need to use the same method but just pass in a different parameter, so in that case, won't it make sense to use a helper in a controller? ...

POSTGRESQL Effective way to split table performance-wise

I have a user table in postgress. Each user can have from 0 to many websites. I know that it will be waste of memory to bring the user-websites everytime I get the user object from the database and of course I cant know how many websites the user will have. I could have a table called websites but then I think this could happen again w...

How to use model without a controller in Ruby on Rails?

I'm just learning Ruby on Rails (no prior Ruby experience) I have these models (not showing the migrations here for brevity - they're standard fields like firstname, city etc): class User < ActiveRecord::Base has_one :address end class Address < ActiveRecord::Base has_one :user end How do I use the Address class to manage the und...

Ruby-Rails serve ftp file direct to client

Hello! I am new to ruby and to rails, so excuse my question... . What i want to know is, how to take a file from a ftp server with ruby without saving the file on my rails application harddrive (streaming the filedata direct to the client). I am working with the ruby Net/FTP class. With the method "retrbinary" from the Net/FTP class i ...

Given the following Model, how to determine the User's Role?

Given the following model: class User < AR::B has_many :permissions has_many :projects, :through => :permissions end class Project < AR::B has_many :permissions has_many :users, :through => :permissions end class Role < AR::B has_many :permissions end class Permission < AR::B belongs_to :user belongs_to :proje...

Using YAJL to render json in Rails 2.3.4

I want to use YAJL to render JSON. If I require 'yajl' in the controller and perform a render :json => some_hash, will this use YAJL to encode the hash into json? Should I convert the hash to JSON using YAJL's encode method before calling render? If not, how can I go about doing this? ...

Is there a plural/singular reference list for Rails?

Ruby on Rails has many different generators and other such things. In my experience, the naming is hardly ever obvious though for if you should use a singular or plural name. For instance for the Controller generator you are suppose to use plural $ rails generate controller Users new But for Models you are suppose to use singular(for...

Rails find and sort using natural sort order collation

I want to return a paginated list of countries with Åland coming after Azerbaijan instead of after Zimbabwe. In other words, I want to ignore special characters and simply treat an "Å" as an "A" and the "ô" in Côte d'Ivoire as a regular "o". Is there a Rails method or gem to do this or do I need to execute some kind of custom SQL (and if...

How does MongoDB compares the date only and ignores the time, such as date <= '2010-09-10' ?

For some reason: Analytic.where({:ga_date.gte => '2010-09-01'}).count() # greater than or equal to gives back 0, but Analytic.where({:ga_date.gte => Time.parse('2010-09-01')}).count() gives back 230, which is the number of records (documents). Actually, the first line on the top works in another case, so it is quite strange. Ca...

Prevent gem A from overriding methods of unrelated gem B

For some time, xml-simple gem had been working for me just fine (indirectly, through another gem). But lately I had to install Amazon S3 gem also. Amazon guys had decided that xml-simple wasn't cool enough, so they supplied a replacement: 'faster-xml-simple'. And they also decided that everybody wants to use their code now, so they did ...

Does Mongoid have Map/Reduce?

I am using Ruby code to calculate sum from the array returned by Mongoid. But maybe using Map/Reduce can be faster, except I don't see any docs for Map Reduce on mongoid.org and Google for map reduce site:mongoid.org doesn't give any result either. (or using MapReduce or Map/Reduce) There are docs on MongoDB's site map reduce site...

Can I use a redirect_to in a Rails controller with JQTouch?

I have a UserSessions controller whose create action issues a "redirect_to dashboard_path" for the mobile mime type. The form is being submitted with a link_to, which JQTouch turns into an ajax request. JQTouch ends up sliding in a completely blank page with the #undefined anchor appended to the URL. I've determined that what it's doing ...

Getting crazy over Rails 3, RVM, gems

This question is really hard for me to describe, so any improvements on it would be nice. I am currently on Ubuntu 10.4, I have installed RVM (probably as root, that could be my mistake) I did what this guide told me to do: http://rubyonrails.dreamwidth.org/1713.html and from my point of view it worked. I was able to create a project ...

FB.login redirect after authentication

Hi, How do I set a redirect url for FB.login? I already have this in to render the login popup: function fbLogin () { FB.login(function(response) { if (response.session) { if (response.perms) { window.location = "redirect_url_here" } else { // user is logged in, but did not grant any permi...

Ruby: How do you convert a time or date to a friendly url string?

For instance, if I have a Time.now, how would I convert that to a friendly url string? Time.now.some_method_here => some_url_friendly_string_here I believe there is a built-in Ruby method to do so, but I can't seem to locate it on Google. Any ideas? ...

Does Rails help in learning PHP Zend Framework

I'm starting to learn Zend Framework and it reminds of when I dabbled with Rails a while back. Does knowledge of Rails help with better understanding Zend and the whole controllers, model, views architecture? Zend feels to me like new PHP, Rails-style. There are a lot of people talking about the steep learning curve, so I was thinking ...

Rails authenticate_or_request_with_http_basic

When a user tries to connect via this method and it fails. How do I redirect_to? Thanks in Advance. class ApplicationController < ActionController::Base protect_from_forgery USER_NAME, PASSWORD = "admin", "admin" helper_method :authenticate private def authenticate begin authenticate_or_request_with_http_basic...

How to copy a (Active)record between tables, partially?

In two tables mapped to ActiveRecord with unknown number of identical columns, e.g.: Table A Table B --------- --------- id id name name age email email is_member How can I (elegantly) copy all identical attributes from a record of Table A to a record of Table B, except the id attri...

Rails Authorization with CanCan Problem

On my rails app I have implemented AuthLogic and CanCan. However when trying to figure out if the user can manage an article (checks if he is owner through the article.user_id) with CanCan I am running into issues. This should be straight forward I don't know what I'm doing wrong. User has_many Articles class Ability include CanCa...