activerecord

preventing multiple active record processes from inserting duplicate records

class State < ActiveRecord::Base validates_uniqueness_of :identifier end Now assume there are two running processes that can possibly insert two of these records simultaneously. Passenger is a good example. Here's a contrived one: State.find_by_identifier("UNIQ").delete rescue nil while Time.now < Time.now.change(:hour => 17, :min...

rails3 session store

Hi, could you tell me plz - how to use in rails3 application external Active Record session store? In rails2 its simply ActiveRecord::SessionStore::Session.establish_connection("sessions_#{RAILS_ENV}") but wat about rails3? ...

accepts_nested_attributes_for with find_or_create?

I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it not create new records if a record already exists? By way of example: Say I've got three models, Team, Membership, and Player, and each team has_many players through memberships, and players can belong to many teams. The Team model might th...

Array.count works fine locally but breaks on heroku

Right now, if I go to the index action of a model that I have, I don't show the basic table of data that rails generates for me if there are no existing records in the database. I do something like: <% if @my_records.count > 0 %> <table> <tr> <th>...</th> <th>...</th> <th>...</th> <th>etc</th> </tr> <% @my_records....

RoR: How to handle custom nested form's submit

Hello! I managed to do almost all the way towards happiness with my custom form in ruby-on-rails, but the very last step is missing and it is impossible to find the answer on the net because of too many common words. I believe that the answers to my questions are trivial for people who have done RoR for a time, but be warned that the p...

Can I create a Generic delete method to delete any subsonic activerecord<T> object?

We have a C# WinForms app with a number of screens that support CRUD operations. We've been trying to create a generic method in the base form class to delete any subsonic ActiveRecord class. This has proved to be a challenge. calling Delete() for a subsonic type involves calling the static method on the abstract base ActiveRecord<T> ...

Rescue and redirect 500 Error in Rails when Database is not found

We had some patches for our Database applied the other day. I was not given a heads up, and the application went down. We are getting nothing but a white Status: 500 Internal Server Error Content-Type: text/html 500 Internal Server Error page. We are using Rails 2.2.2 I want to redirect the user to /500.html so that they get th...

Subclassing ActiveRecord with permalink_fu in a rails engine

This question is related to extending class methods in Ruby, perhaps more specifically in the way that permalink_fu does so. It appears that has_permalink on a model will not be available in a derived model. Certainly I would expect anything defined in a class to be inherited by its derived classes. class MyScope::MyClass < ActiveRecor...

[ActiveRecord 3] how can I make an 'OR' statement

Hey, I am trying to create an 'OR' sql statement in ActiveRecord 3, I tried all kinds of variations but cant figure it out... I am new to it so I might be missing something obvious. For example I want this query to include multiple 'channel_ids' and have it return all posts for any of the channel IDs. This works for one: Post.where(...

Overridable Active Record Fields

I have a cms and public facing website built in rails that pulls in an XML file (from another system) every hour that contains all our product data. But once in a while, the marketing team might want to override certain fields in order to display them differently on the website. For example, they may want to alter the name of a product...

Using Active Record on object split across two tables in codeigniter

I have a controller that is represented in the database by two tables, pages and page_contents. pages contains the author information, unique id for the the page, and creation timestamp. page_contents contains the page uid as a FK, the contents of the page, last editor, and edit timestamp. When a page it requested I need to pull the o...

Inherit active_record in rails.

Right now my classes are look like this. class BalanceName < ActiveRecord def before_validation set_blank_attributes_to_nil(@attributes) end end class Balance < ActiveRecord def before_validation set_blank_attributes_to_nil(@attributes) end end I want to inherite activer record into one class and than want...

Group Query with Calculations on Rails 3

Rails 3 problem. I have a Foods table of foods with the following attributes: name calories (per gram) fat (per gram) carbs (per gram) protein (per gram) I then have a LoggedFoods table representing a food that has been eaten at a given time. It has the following attributes: food_id number_of_grams_eaten ate_when (datetime) So th...

How to use :conditions to compare (a datetime) with (datetime + 'number of days') ?

I'm building an application that checks when a user needs to change the oil of one of his cars. Here's how it works: The user enters the name of his car (e.g. 'Ford Mustang') The user enters the time the car will run on one oil change (e.g. 3.months) The user saves the car to the database Repeat above process for all his other cars. (Y...

How to insert record to an association table with PRADO ActiveRecord?

Hi! I have 2 many-to-many tables and 1 association table for them: 1. Student StudentID (PK) 2. NextOfKin NextOfKin (PK) 3. StudentNextOfKin StudentID, NextOfKinID (PK) FK StudentID References Student(StudentID) FK NextOfKinID References NextOfKin(NextOfKinID) With the following codes, i am able to create a new NextOfKin record in D...

Merge two ActiveRecord arrays and order by created_at

books = Book.find(:all) articles = Articles.find(:all) By reading from http://guides.rubyonrails.org/layouts_and_rendering.html I knew that I could do something like: <%= render :partial => [customer1, employee1, customer2, employee2] %> and it would use _customer and _employee partials as appropriate. So I want to do something lik...

Combine two ActiveRecord Query results

I currently have two active record queries that I would like to combine together joins("join relationships ON user_id = followed_id"). where("follower_id = #{user.id}") and where(:user_id => user.id) Basically I want the results of the second one to appear with the first similar to a UNION statement in SQL. Can it b...

Changing Rails Query to Pure SQL

I'm looking to move the following code away from ActiveRecord to pure SQL for a performance increase. What would be the best way to write this query in pure SQL (MySQL DB)? User.count(:conditions => ["email = ?",params[:email]]) > 0 Thanks ...

mysql timeout error in rails 3 when using delete_all and make! in step definition

I'm using following construct in a cucumber step definition. Given "I have following stuff" do Model.delete_all list.each { |i| Model.make!(:name => i) } end (make! is from machinist 2). Above step fails with INSERT statement timeout. When I open up a console for test, environment, I can execute each statement without an issu...

How to order a query by a translated field using globalize

I'm trying to order a query using a field which is translated with globalize2. The problem is that since stored in database and in an association I'm having lot of problems. Doing an include of the translations and ordering by category_translations.name doesn't work. I tried a default_scope but since it doesn't allow to use lambda or a...