activerecord

Database locking: ActiveRecord + Heroku

I'm building a Sinatra based app for deployment on Heroku. You can imagine it like a standard URL shortener but where old shortcodes expire and become available for new URLs (I realise this is a silly concept but its easier to explain this way). I'm representing the shortcode in my database as an integer and redefining its reader to give...

Does Castle ActiveRecord's ActiveRecordMediator<> support LINQ?

Does Castle ActiveRecord's ActiveRecordMediator<> class support LINQ queries? I couldn't find any method that returns an IQueryable<>. My domain objects can't inherit from ActiveRecordLinqBase<> due to their design. ...

ActiveRecord and transactionsin between `before_save` and `save`

I have some logic in before_save whereby (only) when some conditions are met I let the new row be created with special_number equal to the maximum special_number in the database + 1. (If the conditions aren't met then I do something different, so I can't use auto-increments) My worry is that two threads acting on this database at once m...

what is difference between attr_accessible(*attributes) & attr_protected(*attributes)

Hi All, I want to know what the difference is between attr_accessible(*attributes) & attr_protected(*attributes) with examples if any. I see many developers use these in their models. I Googled for the differences but I don't get exactly what they are. Could someone explain to me the importance and its necessity in different scenarios....

using UUID as primary key in rails and polymorph relationships

I'm creating a rails 3 application that will be decentralized and I need to use UUID as primary key for my tables, what would be the best gem, plugin for the Job. I also would like to know if it is possible to make in ActiveRecord polymorphic relationships without using the polymorphicable_type column for it, given the case that I'm usin...

ActiveRecord finding from inside a serialized field

While working with ActiveRecord I have a table which stores a serialized array of participant usernames for each row in one field. Is there an easy way to search for all rows who contain a specific user? class Thing < ActiveRecord::Base serialize :participants end I realise I could just make a new linked table for the participants, ...

What is the equivalent of <composite-element> in Castle ActiveRecord?

I have a TrackLog that has a collection of TrackPoints: public class TrackLog { public string Name { get; set; } public ISet<TrackPoint> TrackPoints { get; set; } } public class TrackPoint { public DateTime Timestamp { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } } I'd lik...

Activerecord join search

Hi, i have two models: class Category has many :jobs end class Job belongs_to :category end So for sure i'm able to do c = Category.first c.jobs My question is: how can i find just categories that has at least one job? I just forgot to add today i'm executing it like: Category.find(:all).collect { |x| x if x.jobs.size > 0 }...

tag statistics with rails

I am using the plugin: http://github.com/karmi/is_taggable How can I perform simple statistics on my tags, e.g. what's the most used tag? which tags are not used, etc.? With SQL I would do something like: select id, count(*) from taggings group by taggable_id; But I am not seeing how to do this with the plugin. Thanks! ...

Storing old previous year data in Rails?

Hi, I'm developing an app which has massive data entries. Its like Campaign which has attrs like rate_per_sq_feet, start_date, end_date. i.e it will have max date of around 30 days. Once the campaign is finished, its done and another starts. Now I'm confused that how to store those campaigns as reports so that its not accessed regurlarl...

How to handle this type of model validation in Ruby on Rails

I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner has_many :dogs has_many :cats Not the best example, but again, it demonstrates what I'm trying to solve. Now when a request comes in as an HTTP POST to http://127.0.0.1/pets, I want to create an instance of Pets. The restriction...

Does ActiveRecord make Ruby on Rails code hard to test?

I've spent most of my time in statically typed languages (primarily C#). I have some bad experiences with the Active Record pattern and unit testing, because of the static methods and the mix of entities and data access code. Since the Ruby community probably is the most test driven of the communities out there, and the Rails ActiveReco...

Rails - using :include to find objects based on their child's attributes

I have a sentence and correction model class Sentence < ActiveRecord::Base has_one :correction class Correction < ActiveRecord::Base belongs_to :sentence and I'm trying find all sentences which don't have a correction. To do this I'm simply looking for corrections which don't exist i.e. whose id = nil. But it is failing and i ...

PostgreSQL activerecord weirdness for .maximum

I have an ActiveRecord before_save method throwing an odd error: class MyThing < ActiveRecord::Base before_save :dostuff def dostuff p self.class.maximum(:mycolumn) end end When I was using SQLite this worked perfectly, now—with postgresql—I get the error: PGError: ERROR: current transaction is aborted, commands ignored un...

rails: has_many with 2 relations fields

Hi I have a relation between 2 models. The models are "Category" and "Page". I have now the use case that I need 2 Pages/Category. How can I write this? The fields in Category are "page_id" and "page_en_id". I'm not sure which is the best solution for this, I only know the belongs_to and has_many solution with foreign key option which...

Want to store profiles in Qt, use SQLite or something else?

I want to store some settings for different profiles of what a "task" does. I know in .NET there's a nice ORM is there something like that or an Active Record or whatever? I know writing a bunch of SQL will be fun ...

ActiveRecord + CodeIgniter - Return single value from query, not in array form.

Say you construct an activerecord query that will always just return a single value, how do you just address that single value instead of getting an array in return? For instance I am using an ActiveRecord query to return the SUM of a single column, it will only return this one single SUM, instead of having to parse the array is there a ...

Saving an active record, in what order are the associated objects saved?

In rails, when saving an active_record object, its associated objects will be saved as well. But has_one and has_many association have different order in saving objects. I have three simplified models: class Team < ActiveRecord::Base has_many :players has_one :coach end class Player < ActiveRecord::Base belongs_to :team valid...

Rails ActiveRecord: Find All Users Except Current User

I feel this should be very simple but my brain is short-circuiting on it. If I have an object representing the current user, and want to query for all users except the current user, how can I do this, taking into account that the current user can sometimes be nil? This is what I am doing right now: def index @users = User.all @user...

How to test ActiveRecord callbacks with RSpec?

How to test the following example? class Post < ActiveRecord::Base belongs_to :discussion def after_save # or after_create discussion.touch end end EDIT: I have found a nicer way to write the code above. class Post < ActiveRecord::Base belongs_to :discussion, :touch => true end ...