activerecord

Relationship among models[Rails]

Hi I am Having 3 Models Category / Issue / Article Where Article is taggable_on :topics and :sub_topics Using [acts_as_taggable_on plugin] So what kind of relationship we have to maintain among these model and how we can retrieve category topics,sub_topics / issues topics and sub_topics Thanks in advance ...

How convert a string into a ActiveSupport::TimeWithZone?

How convert a string into a ActiveSupport::TimeWithZone? My dilemma is this. I need to update a field (called updated_at). The field in mysql is datetime, and the class is ActiveSupport::TimeWithZone. But the dates are strings like "10/17/2008". I used "10/17/2008".to_date (And I intend .to_time and to_datetime), and even if in console ...

Rails Resque workers fail with PGError: server closed the connection unexpectedly

I have site running rails application and resque workers running in production mode, on Ubuntu 9.10, Rails 2.3.4, ruby-ee 2010.01, PostgreSQL 8.4.2 Workers constantly raised errors: PGError: server closed the connection unexpectedly. My best guess is that master resque process establishes connection to db (e.g. authlogic does that when...

Should I be using callbacks or should I override attributes?

What is the more "rails-like"? If I want to modify a model's property when it's set, should I do this: def url=(url) #remove session id self[:url] = url.split('?s=')[0] end or this? before_save do |record| #remove session id record.url = record.url.split('?s=')[0] end Is there any benefit for doing it one wa...

Multiple children in single form in rails

I have a model that has an arbitrary number of children entities. For simplicity lets call the entities Orders and Items. I would like to have a create Orders form where I input the order information, as well as add as many items as I want. If I click the "Add another item" button, a new set of form elements will be added to input the ne...

Rails + simple role system through associative table

So I have the Ninja model which has many Hovercrafts through ninja_hovercrafts (which stores the ninja_id and the hovercraft_id). It is of my understanding that this kind of arrangement should be set in a way that the associative table stores only enough information to bind two different classes. But I'd like to use the associative tab...

Can't establish a DB connection. (Maybe threads related)

Hello, I am getting this exception on the Rufus::Scheduler ActiveRecord::ConnectionTimeoutError: could not obtain a database connection within 5 seconds. The max pool size is currently 30; consider increasing it. I've increased the pool from 10 to 30. And I also monkey patched the method which raises the exception to also call Active...

Rails: id field is nil when calling Model.new

I am a little confused about the auto-increment id field in rails. I have a rails project with a simple schema. When i check the development.sqlite3 I can see that all of my tables have an id field with auto increment. CREATE TABLE "messages" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "text" text, "created_at" datetime, "updated_...

PostgreSQL + Rails citext

I am trying to move to heroku which uses PostgreSQL 8.4 which has a citext column type which is nice since the app was written for MySQL. Is there any way to use :citext with rails (so that if the migrations are run on MySQL the citext would just use string/text? I found this ticket, but it seems like it isn't going to be a part of rai...

How to cache queries in Rails across multiple requests

I want to cache query results so that the same results are fetched "for more than one request" till i invalidate the cache. For instance, I want to render a sidebar which has all the pages of a book, much like the index of a book. As i want to show it on every page of the book, I have to load it on every request. I can cache the rendered...

How do I order by foreign attribute for belongs_to reference where there are 2 keys to foreign table

I have a Model which has a belongs_to association with another Model as follows class Article belongs_to :author, :class_name => "User" end If I wanted to find all articles for a particular genre ordered by author I would do something like the following articles = Article.all(:includes => [:author], :order => "users.name") Howeve...

In Rails: How can I turn off caching for a particular activerecord database table?

I have an associative table (scripts_runs) (has_many, through) that carries a status column ('started', 'ready to parse', 'completed' ). The started and ready to parse states are signaled by 'flag' files from distributed executions of the scripts pickedup periodically (20 seconds). The completed status is written directly to the assoc...

Way to view Rails Migration output

Is there an easy way to see the actual SQL generated by a rails migration? I have a situation where a migration to change a column type worked on my local development machine by partially failed on the production server. My postgreSQL versions are different between local and production (7 on production, 8 on local) so I'm hoping by lo...

Strange problem with ActiveRecord summing grouped values

I'm working on a timesheet system (thrilling, I know) and am having difficulty using ActiveRecord to total the time spent working on particular projects on each day. I have a TimeBooking model: t.references :assignment, :null => false # (Combination of employee and project) t.date :date, :null => false t.integer :durat...

Getting ActiveRecord (Rails) to_xml to use xsi:nil and xsi:type instead of nil and type

The default behavior of XML serialization (to_xml) for ActiveRecord objects will emit 'type' and 'nil' attributes that are similar to XML Schema Instance attributes, but aren't set in a XML Namespace. For example, a model might produce an output like this: <user> <username nil="true" /> <first-name type="string">Name</first-name> <...

Including associations optimization in Rails

Hey, I'm looking for help with Ruby optimization regarding loading of associations on demand. This is simplified example. I have 3 models: Post, Comment, User. References are: Post has many comments and Comment has reference to User (:author). Now when I go to the post page, I expect to see post body + all comments (and their respectiv...

Accepts Nested Attributes For - edit form displays incorrect number of items ( + !map:ActiveSupport::OrderedHash {} )

Hi, I have a teacher profile model which has many subjects (separate model). I want to add subjects to the profile on the same form for creating/editing a profile. I'm using accepts_nested_attributes for and this works fine for creation. However on the edit page I am getting a very strange error - instead of seeing 3 subjects (I added t...

Why each request with activerecord is doing SHOW TABLE?

My test page is processed if believe to trace in 46 ms, while 11 of them I am doing this 20:53:06.111597 system.db.CDbConnection Opening DB connection 20:53:06.118046 system.db.CDbCommand Querying SQL: SHOW COLUMNS FROM `questions` 20:53:06.122476 system.db.CDbCommand Querying SQL: SHOW CREATE TABLE `questions` Is this ...

Activerecord default accessors & unusual requirements

I have an ActiveRecord::Base class which needs to have one field's value picked as the lowest integer available considering the records already in the database. This snippet does that, does it seem efficient to you? Can it be improved? class Thing < ActiveRecord::Base def initialize special = 0 Thing.find(:all,:order=>"special...

Ruby & ActiveRecord: referring to integer fields by (uniquely mapped) strings

While its not my application a simple way to explain my problem is to assume I'm running a URL shortener. Rather than attempt to try and figure out what the next string I should use as the unique section of the URL, I just index all my URLs by integer and map the numbers to strings behind the scenes, essentially just changing the base of...