activerecord

validates_presence_of in a module

i have a model. i want import in this model a module. in this module i want insert a validates_presence_of for the models that import it I want know if and how is possible to do something like this: class Ele < ActiveRecord::Base include Mod end module Mod validates_presence_of :field end Thanks ...

Override all table name convention in ActiveRecord

My project doesn't use the plural convention in table's names. How can I override this convention without calling set_table_name in all my ActiveRecord class ...

Is there a NoSQL / key-value store abstraction library like there is JDBC is for databases?

I have used many SQL abstraction libraries, such as ODBC, JDBC, and ActiveRecord. What are the abstraction options in the NoSQL / key-value store world? I am mostly asking this so that if I choose a key-value store then I can use an abstraction library and not be locked in, which I think is important given the number of key value store...

Calling ActiveRecord's #relationship_ids = [1,2,3] saves immediately. Any workarounds?

Hello, I've come across an oddity in ActiveRecord's #relationship_ids method (that's added automatically when you declare 'has_many'), which saves immediately for existing records, which is causing me some issues, and I wonder if anyone had any useful advice. I'm running Rails 2.3.5. Consider this simple scenario, where an article has...

Rails ActiveRecord associations inconsistently updated

I am running into some Rails 2.3.5 ActiveRecord behavior I do not understand. It appears that an object can have its association ids updated in inconsistent ways. This is best explained with an example: Create a Post model with the string attribute 'title' and a Comment model with the string attribute 'content'. Here are the associa...

Rails validations running against original record during update

I'm trying to figure out an inconsistency between what's happening in a functional test and what is happening in my development environment. I have a custom validation method unique_entry that is essentially a specialized version of validates_uniqueness_of. It looks like this: def unique_entry matched_entry = Entry.first(:conditions =...

Looking for records of the current month with a DataMapper query

Hi, I'm trying to reproduce this SQL query I used with ActiveRecord: Post.all(:conditions => ["EXTRACT(MONTH from created_at) = ? AND EXTRACT(YEAR from created_at) = ?", month, year]) But since I'm converting everything to DataMapper I was looking for a way to do this... Can anyone help? ...

activerecord and mongo / mongo-mapper bridge

I have a project which I have used Active Record and which I'd like to add some new features using MongoDB. Rather than re-invent the wheel and re-write my entire site, how can I integrate 2 models together, one which use MongoMapper and the other ActiveRecord (postgres). I've found that others have done it successfully, but no example...

count with has_many in rails

I've got an Order and Orderdetails Orderdetails belongs_to Order Order has_many Orderdetails I am trying to convert the following query to ActiveRecord count function select Count(*) from orderdetails A, orders B where A.prodid='6' and A.orderid= B.id and B.custid='11' I tried: @count = Orderdetail.count(:conditions => "prodid ...

how to override a validation call in activerecord in a gem?

I am using apn_on_rails for iphone push notification with rails. Right now, the validation on token is no longer valid because the validation requires a space every 8 characters: validates_format_of :token, :with => /^[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}$/ http://github...

ActiveRecord query help .count

I have a DB table users that has the following columns: id, clan_id, experience I want to run a query using rails that returns the top 50 clans based on experience. How would you do this query in rails? Clan Table to User Table is a one to many relationship. Thus a clan is composed of multiple users that have the same id. ...

How to implement row ordering in Rails?

I'm trying to implement a UI feature for a listings page where the user can change the order of the records they have created. I'd assume one way to do it would be to store a position field with some kind of editable auto-increment rule; The position values of rows could then be swapped as the user raises or the lowers the position. How...

Is there a Ruby database migration gem, that helps you move content from an old structure to a new structure?

Hi there, Are there any Ruby gems/libraries that help you migrate from an old DB structure to a new structure? ActiveRecord migrations do a good job keeping track of a new db structure, but I wonder if there's something that helps you migrate an entire legacy db to a new structure: transfer_from(:source_table => 'person', :destination_...

Rails: setting column alias attribute with find_by_sql

When I use a column alias in a query with find_by_sql, it doesn't appear to be set in the result objects, even when I add an attr_accessor for the property. class Country < ActiveRecord::Base attr_accessor :average_score def self.sorted_by_average_score sql = "SELECT country_id, AVG(score) AS average_score, countries.name " + ...

In ActiveRecord, how to specify a class attribute that is calculated just once for the duration of the page?

Let's say I have an ActiveRecord called Apples, and I want a class method that calculates the total price of every apple in my database, as so: def Apples.total_price Apples.sum(:price) end This is a method I use in one of my views to make a pie chart. So, something like: Apples.brand("red delicious").sum(:price)/Apples.total_price...

Ruby on Rails Query Help

I currently have the following query User.sum(:experience, :group => "clan", :conditions => ["created_at >= ? and created_at <= ?", "2010-02-15", "2010-02-16"]) I want to return the top 50 clans in terms of experience listed from most experience to least experience with only the top 50 in experience returning. How would I modify the ...

How to access the joining model on a has_many :through relationship

I have a typical many-to-many relationship using has_many => :through, as detailed below. class member has_many member_roles has_many roles, :through => :member_roles end class role has_many member_roles has_man members, :through => :member_roles end class member_role belongs_to :member belongs_to :role # has following f...

How to use Scaffoling from SS2 in SubSonic 3.0?

Hi, I just started using SubSonic ver 3.0 and I am not able to find sufficient info on the scaffolding feature. I understand scaffolding is included in version 2.x but can be used with 3.x also. My question is, what do I have to include in my SunSonic 3.x ActiveRecord project, what to reference and what to add to the web.config / App.co...

efficient bulk update rails database.

I'm trying to build a rake utility that will update my database every so often. This is the code I have so far: namespace :utils do # utils:update_ip # Downloads the file frim <url> to the temp folder then unzips it in <file_path> # Then updates the database. desc "Update ip-to-country database" task :update_ip => :environm...

Rollback active record state: wrong number of arguments (1 for 0)

ArgumentError in SourceController#update_source wrong number of arguments (1 for 0) I try saving a new Article object into the database by writing: @article = Article.new(:title => new_a.title, :description => new_a.description, :source_id => self.id, :url => new_a.link, :pub_da...