activerecord

Can't use ActiveRecord associations Rails 3 in IRB

I have three classes with the following associations: class Technician < ActiveRecord::Base has_many :tickets has_many :services, :through => :tickets end class Ticket < ActiveRecord::Base belongs_to :technician has_many :services end class Service < ActiveRecord::Base belongs_to :ticket belongs_to :technicians end W...

How to avoid ActiveRecord::RecordNotFound exceptions when querying multiple records

I have this code: Article.find([1,2,3]) But only record 1 and 2 exist in the database. I get this exception: "ActiveRecord::RecordNotFound (Couldn't find all Offers with IDs (1,2,3) (found 2 results, but was looking for 3))" Is there a way to only get the existing records and not the exception? ...

Rails 3 - HABTM not updating join table with <<

This is killing me... When trying to add a key to like so: category.site_ids << 1 category.save It does not save. But when overriding completely, it works: category.site_ids = [1] category.save What am I missing here? ...

How to increase max pool size in active record

Error "ActiveRecord::ConnectionTimeoutError - could not obtain a database connection within 5 seconds. The max pool size is currently 5; consider increasing it.:" How do I increase the max pool size ? DB CONNECTION DB_CONN = ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => DB_FILE) ...

One big object or one small plus one big info object?

Hi, For a Ruby on Rails app, I'm having this Movie object which has several attributes. Pretty much every page needs to compare some movies to a full list of these movies sorted by ratings, so I used to cache this big sorted list. It was useful, because I could directly use this list of Movies and all their attributes without any other ...

Rails/ActiveRecord: Get a record made *AFTER* a particular date

So currently I have an issue where I am populating a database from xml files through my client's API. Each record has an expiration date in it, with the following format: <offer_ends_at>2010-10-20T07:59:59-04:00</offer_ends_at> Now, I need to run a query where I only get records that have an expiration in the future. My current code i...

SQL request to find the n last records of each one of a user's friends

Hi, I have a User model which has friends (User.friends with a user_id/friend_id join table called followings). Each user has events (the events table has a user_id column). For an activity feed, I want to get an array with the last 20 events of the friends of the user. Right now I'm calling the last 20 events for each friend, sorting ...

Rails/ActiveRecord: Syntax for "or" conditions?

Hi folks, Just trying to figure out how to write "or" statements in activerecord conditions without resorting to raw sql - I assume it can be done, but google isn't helping much. So, to give an example, suppose there's a model "Author" with a one-to-many relationship with 'Books' and a many-to-one relationship with 'Publisher'. How wou...

Filter by count of children using Rails 3

I'd like to select posts that have one or more comments using Rails 3 and a single query. I'm trying something like this: Post.includes(:comments).where('count(comments.id)>0') However I get this error: ActiveRecord::StatementInvalid: PGError: ERROR: aggregates not allowed in WHERE clause I've googled this and similar approaches,...

Rails 3: DataMapper instead of ActiveRecord

After all the hussle about Rails 3, can I, after all, painlessly use DataMapper without almost changing my ActiveRecord code so that I could run my Rails at GAE without any bother? ...

Should I joint-index an ActiveRecord polymorphic association?

I have a metric table that I expect to be very large. It has a polymorphic association so that it can belongs_to other models that want to record some metric. I typically index association columns like this to speed up association loading. I've heard people talking about joint-indexing this association. This looks like: add_index :c...

how to check status new in activescaffold

active_scaffold :formats do |config| format_order = Format.find(:all, :select => :format_order, :order => :format_order).collect(&:format_order) format_order << format_order.size + 1 # I want only implement when new config.columns = [:name, :format_order] config.columns[:format_order].form_ui = :select config.columns...

What's the best way to make Rails 3 / Active Record associations for classes of a simple point of sale program?

I have 5 classes that are part of a point of sales program: Customer, Technician, Order, Ticket, Service Each Order has one customer and many tickets. Each Ticket has on technician and many services. How should I make the associations so that I can do the following lookup: customer.order.find_by_date(10/20/2010).service.technic...

Rails / ActiveRecord implementing a "Watch list" relationship.

I'm looking for advice on how best to construct a "watch-list" for the app I'm currently working on. Models as follows: # user.rb has_many :items # item.rb belongs_to :user I need to now add a watch-list, where users can favorite certain items, without taking ownership. I've tried the following: # user.rb has_many :items has_many ...

Copy fixed data into model

Hi all, I'm developing an application in which a Course belongs to a user. I would like to predefine a number of courses, such that a Course's template details are then copied into the users course details. From this initial point each user has a one-to-one mapping with a course. I'd like to know the best place for the static attribut...

Trouble structuring MySQL query with one-to-many over multiple tables

I have a blog-style app that allows users to tag each post with topics. I keep this data in three separate tables: posts (for the actual blog posts), topics (for the various tags), and posts_topics (a table that stores the relationships between the two). In order to keep the MVC structure (I'm using Codeigniter) as clean as possible, I...

ignore before_update when save

some case I don't want execute before_update. please help me. case A: in case I want used before_update obj = Object.find(id) obj.save but case B I don't want used before_update obj = Object.find(id) obj.save # in case I want used before_update ...

Rails 3 - Distinct and Random

I am trying to choose 5 distinct brands from among the cars that belong to my users. My current attempt is below (no approach to selecting at random). @range = @user.cars.select('DISTINCT brand_id').limit(5) How might I select these five random distinct brands? Thank you very much. ...

Referencing a belonged_to items owner in Rails / ActiveRecord query.

Lets say I have a Person model and a Opinion model. Opinions belong to Person, a Person has many Opinions. Some people are shy. I am trying to find a set of opinions where the person is 'not shy' or something along those lines. Is there a way to do this that does not involve finding all people who are not shy and then finding those p...

Manipulating ActiveRecords using only a few attributes with Ruby on Rails

Hi, For caching matters, I'm caching an array of the attributes of the objects I need: friends = [{:id => 4, :name => "Kevin"}, {:id => 12, :name => "Martin"}, …] Is it possible to have a list of Users using this array, so that I can use Ruby methods? For instance, I usually get a list of non-friends with this: non_friends = User.al...