ruby-on-rails

how to print out autoincrement ID in Activerecord model

I have a before_save in my model before saving the record to the database...I'd like to print out the autoincremented ID that will be inserted. My table has a column id in it. I tried before_save :printId def printId puts "ID that will be inserted is: " + self.id end this does not work... ...

Strange behaviour in ActiveRecord finder while using SQL condition IN

I have a table called inbox_messages with following rows: user_id message_id ======= ========== 4 8 4 1 4 7 0 9 0 10 0 11 0 12 The table maps to the following model: class InboxMessage < ActiveRecord::Base end When I invoke the find method with a IN caluse, I get different resultset dependi...

should I send status as 404 or 200?

I am using Rails to build a web application. In case a record is not found for a url say http://example.com/city/non_existant_city then I have following code. render :text => "record was not found", :status => :not_found In firefox I see the message. In chrome (on mac) I see a chrome 404 page and this page does not display the messa...

How can i render a json response from a hash while maintaining order in ruby on rails?

hey out there, i am failing to render a json response in ruby on rails from a hash datastructure of country-names with their country-codes: { "AF"=>"Afghanistan", "AL"=>"Albania", "DZ"=>"Algeria", ... }, so that the json response has its entries alphabetically ordered like this: { "AF":"Afghanistan", "AL":"Albania", "DZ"=>"Algeria" ... ...

Rails ActionController Execute Same Code for Every Action

Hello, To the rails experts out there I was wondering where/how you would execute the same code for every action in your web application? If you can point me to an article or provide a short code snippet I would greatly appreciate it. Thanks in advance to anyone who can help. ...

RESTfully destroy polymorphic association in Rails?

How do I destroy the association itself and leave the objects being associated alone, while keeping this RESTful? Specifically, I have these models: class Event < ActiveRecord::Base has_many :model_surveys, :as => :surveyable, :dependent => :destroy, :include => :survey has_many :surveys, :through => :model_surveys end class Model...

Authlogic: Bypass model validation if user is logged in

I'm using Authlogic. I've got an Entry model and there is a validation method that I want to skip if the request was made by a logged in user (anonymous users are also allowed to create entries). I assume that UserSession.find would not be usable from inside the Entry model like it is from a controller. What is the best way to handle thi...

Hooking Observers with Events

Hi, We are using AASM in quite a few of our models, but we're looking at simplifying a bit the models. One of the things we'd like to do is to move all the Notification stuff out of the models and into Observers. So considering: class ClarificationRequest < ActiveRecord::Base include AASM aasm_initial_state :open # States aa...

Creating a multi-step create action in Rails

I have a portion of my app that sometimes will need to inform the user of their action, but the app does not know of the requirement to inform the user until after the form has been submitted (which eliminates Javascript). Basically what happens is that the user uploads one or more files containing records, if the number of records with...

how to dynamically add a value to a drop down in rails

if I have a drop down on a page that lists some values which are in a DB table. next to the drop down I have something which when clicked makes a text box. Kind of like Edit in place. is it possible to type some value there hit enter and have that value be loaded up in the drop down? ...

Passing a local variable to a hidden_field in a partial

Hi I am trying to pass these local parameters to a partial form, and they are being passed to hidden_fields. I can't put the values straight into the forms because they will change. Any help would be greatly appreciated. I have: <% form_for :user, @user, :url => { :action => "create", :controller => "users"} do |f| %> <%= f....

How can I mock/fake the existence of a file using rspec?

This is what I have: it "should be able to get a valid directory path" do @asset.some_file_path.should == "/something/test.jpg" end The problem is that some_file_path returns "/not_here_yet.jpg" if there is no existing file. def some_file_path if File.exists(self.my_image_path) return my_image_path else return "/not_...

Abstracting a date subtraction SQL query with Rails

I'm using the following as part of an ActiveRecord find method: :conditions => "created_at > date_sub(now(), INTERVAL 7 DAY)" This works properly on my development server where I'm using MySQL, but I'm deploying to postgreSQL which doesn't have a date_sub function. Ignoring the fact that I'm using two different databases, does Rails h...

How can I create new symlink for RoR Application in the public_html folder ?

I have one folder in the public_html folder named techease and I want my application symlink with this existing folder how can I do this ? because when I link this with this folder then it will create public folder in the techease folder and then symlink with that folder. It how can I create symlink with the existing folder not new cre...

Test does not load many to many fixture

I am using Test to test my application. I have some fixtures: users.yml, roles.yml, roles_users.yml. The users and roles are loaded but not the many to many table roles_users so the users are not related to any role. With Rails console in development everything is ok, in test any user is not connected to any role. I can see the users and...

How to pull down bank transaction records?

I have noticed that some applications provide functionality allowing the user to pull down transaction records (usually for credit cards) from an online banking system. Usually specific banks are supported. What is the general method to achieve this (i.e., using an API, or something else)? I have seen a web app that supports the bank th...

Block List Question

I have the following code in one of my views: <% @videos.each do |i| %> <tr class="<%= cycle("even","odd") %>"> <td><%= i.title %></td> <td><%= i.premiere %></td> <td><%= i.film_type %></td> <td><%= i.preferred_date %></td> <td><%= i.actual_date %></td> <td><%= i.created_at %></td> <td><%= i...

RoR live-search (text_field_with_auto_complete) submit.

I have a "Movies" and a "Actors" table and "Casts" as join-model. To be more specific "Casts" has movie_id, actor_id and rolename. I want in "Movies" form to add a live search to search through actors and a "rolename" text_field and save those to "Casts". I don't know if text_field_with_auto_complete is the right choice but i prefer not ...

Rails nested form using file_field element

I am using code found at Railscast 197 to create nested forms using jQuery. The challenge I am facing is that the nested model uses a file_field element, and because of this when looking at the edit view I just see a list of empty file_field elements. Is there a way I can tell rails to display something else (like a disabled text_field) ...

Netbeans Intellisense for Rails

Has anybody figured out a way to make the Netbeans intellisense for ruby and rails better? It either has too many options in the list (which I understand is a problem since it is a dynamic language). Or it has no options in the list, as if it is not dynamic enough to find everything. Are there any hacks to make it better, or is this j...