activerecord

Rails / ActiveRecord Modeling Help

I’m trying to model a relationship in ActiveRecord and I think it’s a little beyond my skill level. Here’s the background. This is a horse racing project and I’m trying to model a horses Connections over time. Connections are defined as the Horse’s Current: Owner, Trainer and Jockey. Over time, a horse’s connections can change ...

Sql query Error: Operand should contain 1 column(s) in rails

Hi everyone, SELECT * FROM `jobs` WHERE (SELECT DISTINCT jobs.* FROM jobs, job_requests WHERE (jobs.user_id = 1) OR (job_requests.user_id = 1 AND job_requests.job_id = jobs.id) ) This sql gives me: Mysql::Error: Operand should contain 1 column(s). If I execute the select from the wh...

Case-insensitive find_or_create_by_whatever

I want to be able to do Artist.case_insensitive_find_or_create_by_name(artist_name)[1] (and have it work on both sqlite and postgreSQL) What's the best way to accomplish this? Right now I'm just adding a method directly to the Artist class (kind of ugly, especially if I want this functionality in another class, but whatever): def sel...

Require a specific version of ActiveRecord

I have both Rails 2.3.4 and Rails 3.0.0.beta installed on my local machine. I am using ActiveRecord in a stand alone ruby script and when I do require 'active_record' 3.0.0.beta is loaded. How can I force it to require 2.3.4 instead? (without uninstalling 3.0.0.beta) ...

What's a good PHP Active Record library?

I've been using CodeIgniter for some quite time, and I've been extremely happy with its Active Record stuff. It's great to query the database with it. Recently I've started a new project and I can't use such a framework anymore. Is there a simple PHP Active Record library that does its job and gets out of the way (similar to CodeIgnite...

ActiveRecord Associations Question

I'm new to rails and have volunteered to help out the local High School Track team with a simple database that tracks the runners performances. For the moment, I have three models: Runners, Race_Data and Races. I have the following associations. Runners have_many Race_Data Races have_many Race_Data I also want create the association ...

Problems with :uniq => true/Distinct option in a has_many_through association w/ named scope (Rails)

See updates at bottom of question. I had to make some tweaks to my app to add new functionality, and my changes seem to have broken the :uniq option that was previously working perfectly. Here's the set up: #User.rb has_many :products, :through => :seasons, :uniq => true has_many :varieties, :through => :seasons, :uniq => true has_ma...

Reusing named_scope to define another named_scope

The problem essence as I see it One day, if I'm not mistaken, I have seen an example of reusing a named_scope to define another named_scope. Something like this (can't remember the exact syntax, but that's exactly my question): named_scope :billable, :conditions => ... named_scope :billable_by_tom, :conditions => { :billable => tru...

Finding entries belonging to current month (or other months)

I have a model (Expense) that contains fields like 'cost'. I'd like to iterate through all my expenses to find the sum of the cost for all entries belonging to a particular month. Is there a way to do it in rails directly? Expense.find(:all, :conditions => .....) ...

Ruby Rails _without_ ActiveRecord

Hello, I'm looking for any pointers on how to write a rails web app without ActiveRecord. A doc or an example of a (not too complex) web app using storage backends other than a relational database would be greatly appreciated. It's not clear on what should be implemented in the model classes in order to make the rails app work without ...

Rails - Active Record :conditions overrides :select

I have a fairly large model and I want to retrieve only a select set of fields for each record in order to keep the JSON string I am building small. Using :select with find works great but my key goal is to use conditional logic with an associated model. Is the only way to do this really with a lamda in a named scope? I'm dreading that ...

Rails transaction: save data in multiple models.

my models class Auction belongs_to :item belongs_to :user, :foreign_key => :current_winner_id has_many :auction_bids end class User has_many :auction_bids end class AuctionBid belongs_to :auction belongs_to :user end current usage An auction is displayed on the page, the user enters an amount and clicks bid. Controll...

Rails relation select

Hi, I have the following models: class User < ActiveRecord::Base has_many :results, :dependent => :destroy has_many :participants, :dependent => :destroy has_many :courses, :through => :participants end class Course < ActiveRecord::Base has_many :tests, :dependent => :destroy has_many :participants, :dependent => :d...

Is CodeIgniter's ActiveRecord resource intensive?

I’m coding an application with server resources in mind, so I don’t want to use too much so that this application scales in the future. I don’t mind writing my own queries. So, is ActiveRecord resource intensive for my application? Or does it not make a difference? Thanks! ...

How do I get save (no exclamation point) semantics in an ActiveRecord transaction?

I have two models: Person and Address which I'd like to create in a transaction. That is, I want to try to create the Person and, if that succeeds, create the related Address. I would like to use save semantics (return true or false) rather than save! semantics (raise an ActiveRecord::StatementInvalid or not). This doesn't work because ...

How to add additional condition to the JOIN generated by include?

I want to add additional criteria to the LEFT OUTER JOIN generated by the :include option in ActiveRecord finder. class Post has_many :comments end class Comment belongs_to :post has_many :comment_votes end class CommentVote belongs_to :comment end Now lets say I want to find last 10 posts with their associated comments an...

Obtain the stored values of a modified Rails model object

Is there a way to obtain the stored value, that is, the one in the database once the instance has been modified? The problem I'm trying to solve is get the stored slug of an object that failed to save to be able to regenerate the URL. ...

Virgin STI Help

I am working on a horse racing application and I'm trying to utilize STI to model a horse's connections. A horse's connections is comprised of his owner, trainer and jockey. Over time, connections can change for a variety of reasons: The horse is sold to another owner The owner switches trainers or jockey The horse is claimed by a new ...

Delete records from table which matches the data in an array?

I have a table of 2 fields. Word and timestamp. Then i have this array which contains some words. How do i delete all the records in the table which match with the words in the array? Suppose that the model is called "Word". Any ideas on how to achieve this? maybe loop through the array and run some destroy queries. Can anybody direct m...

Querying MySQL with CodeIgniter, selecting rows where field is NULL

I'm using CodeIgniter's Active Record class to query the MySQL database. I need to select the rows in a table where a field is not set to NULL: $this->db->where('archived !=', 'NULL'); $q = $this->db->get('projects'); That only returns this query: SELECT * FROM projects WHERE archived != 'NULL'; The archived field is a DATE field. ...