activerecord

How do I re-use named scopes?

Hi I have a named_scope in my User model as following. named_scope :by_gender, lamdba { |gender| { :conditions => { :gender => gender } } } I want to create other two named scopes which re use this one something like, named_scope :male, lambda { by_gender('male') } named_scope :female, lambda { by_gender('female') } Any idea what...

Treacherous ActiveRecord behavior?

I have a Post class with a vote method which creates a Vote instance This doesn't work def vote(options) vote = self.votes.create(options) return vote if vote.valid? nil end This does work def vote(options) options[:post] = self vote = self.votes.create(options) return vote if vote.valid? nil end Shouldn't the...

Count and Select Object in ActiveRecord with 1 query

We have objects that we want to represent in stacks (think of stacking items in an MMO). There will be duplicate rows. Let's say our owned_objects table looks like this. user_id | object_id 1 | 27 1 | 27 3 | 46 3 | 46 5 | 59 I want the query to do SELECT user_id, object_id, count(*) AS count FROM ...

How to create mysql triggers using migrations in Rails ?

Is there a way to create mysql triggers using Activerecord migrations ? Has anybody worked on it share your experience . Thanks ...

Loading ONE record for has_many and checking it

I'm implementing a Blog with Post and votable Comments. When loading a Post, I want to eagerly load all votes by the current user for the Post's Comments. Something like this (which doesn't work): @post.comments.all(:joins => :votes, :conditions => ['votes.user_id = ?', current_user.id]) Each Comment has a method called rated_by? d...

Is it possible to use the same class with multiple instances connecting different databases with ActiveRecord?

AS it reads I would like to use the same model connecting to different databases, something like this: require 'rubygems' require 'active_record' require "active_resource" dbs = [ 'db_one' ] $config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml')) #Tables we use from 3db class My3DB < ActiveRecord::Base self.abstr...

Multiple column foreign keys / associations in ActiveRecord/Rails

I have badges (sorta like StackOverflow). Some of them can be attached to badgeable things (e.g. a badge for >X comments on a post is attached to the post). Almost all come in multiple levels (e.g. >20, >100, >200), and you can only have one level per badgeable x badge type (= badgeset_id). To make it easier to enforce the one-level-p...

Automatically handle missing database connection in ActiveRecord?

With the launch of Amazon's Relational Database Service today and their 'enforced' maintenance windows I wondered if anyone has any solutions for handling a missing database connection in Rails. Ideally I'd like to be able to automatically present a maintenance page to visitors if the database connection disappears (i.e. Amazon are do...

efficiently storing "answers"

I'm building out an Answer ActiveRecord class that can have different types of answers. That is, the answer could be an integer, float, date, string... whatever... The way I see it there are two ways to store the answers 1) Have an attribute named "value" that is serialized. This is nice since you can always access the answer from ...

Rails ActiveRecord Problem With Complex Join - Select Does Not Work

Hello, I have two model objects: Event Venue Events have Venues. Venues can have 1..* events. Venues have a location, a lat and long, which I use with the Geokit Rails plugin. Here's what these models look like in Rails: class Event < ActiveRecord::Base belongs_to :venue acts_as_mappable :through => :venue end and class Ven...

Conditions with Bind Variables and Optional Parameters

Let's say I have a form where users can search for people whose name starts with a particular name string, for example, "Mi" would find "Mike" and "Miguel". I would probably create a find statement like so: find(:all, :conditions => ['name LIKE ?', "#{name}%"]) Let's say the form also has two optional fields, hair_color and eye_color ...

How to use ActiveRecord in a ruby script outside Rails?

I have a small ruby script in which I'd like to use ActiveRecord to easily access a database model. What is the best way to do it? ...

default_scope with :joins and :select

I tried to define a default_scope in the following way: default_scope :joins => :product, :select => "catalog_products.*, products.*" What I'm getting from Rails though is this: SELECT catalog_products.* FROM `catalog_products` INNER JOIN `products` ON `products`.id = `catalog_products`.product_id When I define it as a named_scope...

Why won't Rails deserialize my field?

I am using the Classifier:Bayes as part of a model class. I have the class set up to serialize the classifier to the db. class Foo < ActiveRecord::Base serialize :classifier end The yaml appears in the db just fine after doing some training and saving the object. But when I query for the class, instance.classifier is a string @f...

Rails active record object causes error when i try to access any attribute

I have code like: @notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>"deliver_by >= '#{Date.today.to_s(:db)}'") logger.info @upcoming_reminders[0].inspect logger.info @upcoming_reminders[0].class logger.info @upcoming_reminders[0].id logger.info "--------------------------...

CodeIgniter: Unknown column xxx in ‘where clause’

Hello There, I am working on a bug(?) for a few hours now, but couln’t fix it. This is my code: if(!$this->db->get_where('merken',array('m_merken' => $brand))->count_all_results()){ $insetData = array('m_name' => $brand); $this->db->insert('merken', $insetData); } $brand contains ‘Acer’ in this preview. A Database ...

Condition on a sub table in rails

Hello I have no good title for this question, but my problem is to set a conditon for my sub-table in rails I have a model named "users" and another named "hours", hours is set to "belongs_to :users" and the users-model has "has_many :hours" But my problem is when I try to fetch the users, but just the hours added this month. I want thi...

MySQL connection timeout - MySQL server has gone away (Sinatra, ActiveRecord)

Hi, Here is an outline of my app: require 'sinatra' require 'active_record' ActiveRecord::Base.establish_connection( :adapter => "mysql", host => $DB_HOSTNAME, :database => $DB_NAME,:username => $DB_USERNAME,:password => $DB_PASSWORD) class Sometable < ActiveRecord::Base end get '/' do #stuff with Sometable end # a lot ...

How to make ActiveRecord work with legacy partitioned/sharded databases/tables?

Hi all, thanks for your time first...after all the searching on google, github and here, and got more confused about the big words(partition/shard/fedorate),I figure that I have to describe the specific problem I met and ask around. My company's databases deals with massive users and orders, so we split databases and tables in various ...

ActiveRecord custom insert/update query

I'm using PostGis to store some spatial data. I used ActiveRecord to retrive data from db, and I used [Property (Formula = "asbinary(shape)")] on property where geometry was stored (property type was byte[]). However this doesn't work when inserting data. So I figured out to write custom insert (and update) queries to solve the problem. ...