activerecord

Rails 3 - Eager loading on a legacy database.

I have inherited a web app with the following tables: Categories, SubCategories and Pages. Pages has category_id and sub_category_id columns. I need to write an efficient query to eager load pages by category and sub_category for iterate in my view (crude example follows): - Category One (categories.each do |category|...) -- Page One ...

How to apply this filter in Rails

Hi, I have a table called as language which has a column called as lang_code. It has the following values. id lang_code created_at updated_at 1 ARA 2010-07-29 15:27:25 NULL 2 CHI 2010-07-29 15:27:25 NULL 3 DAN 2010-07-29 15:27:25 NULL 4 DEU 2010-07-29 15:27:25 NULL 5 ESP 201...

ActiveRecord Callback - How to create a new record based on record just saved?

I need help on what I think should be an ActiveRecord Callback. What I am trying to achieve is, each time a particular record is saved, ActiveRecord actually saves 2 records in the same table. I will stay away from why I want this, it should be good enough to know that each transaction needs 2 separate but similar records saved in the ...

ActiveRecord siblings in many-to-many relationship

I have this working to a degree, but I am looking for some input on how to query for the siblings in a one to many relationship to see if there is a more elegant way of accomplishing this. Consider the following classes class Post < ActiveRecord::Base has_many :post_categories has_many :categories, :through => :post_categories en...

MySQL COUNT results getting interpreted in RoR as a string instead of as an integer

Here's my query for the curious. Only the first and last lines should bear on this question: SELECT s.parent_id AS id, COUNT(s.parent_id) as count FROM items i LEFT OUTER JOIN (SELECT item_id, user_id, MAX(created_at) as created_at FROM item_views iv WHERE iv.user_id = ? GROUP BY item_id) AS v ON i.id = v.item_id LEFT ...

Rails — raise ActiveRecord::RecordNotFound (or any 404 status) for invalid date

In my controller controller, I have use Date.new to create a date object to be passed in my ActiveRecord. end_range = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i).end_of_day.to_formatted_s(:db) The problem with this above is that if a user tried to change the parameters in the URL manually, such as enteri...

Rails relational issue - Find user's next outing

I have an outings table, which basically holds some information about an 'outing'. These outings then have crews, which have users (through crew_users) Just as a note, outings have many crews, so there's an outing_id in the crews table Now, I need to find the user's next outing, as in the first outing where outing.start_time > Time.n...

Sqlite on rails problem with relations

I have a problem with relations while using sqlite3 on rails. First i build my scaffolds, add the references to migration files, add belongs_to has_many to models than get my database up and runinig with basic rake db:migrate command. And then it doesn't work, I guess there is a missing step which i cannot figure out :S By the way i ...

multiple HABTM relationships in rails

class Book < ActiveRecord::Base has_and_belongs_to_many :categories has_and_belongs_to_many :users end class Category < ActiveRecord::Base has_and_belongs_to_many :books end class User < ActiveRecord::Base has_and_belongs_to_many :books end Above is how I declared the relationships between my models. In my book controller a...

searchlogic or ActiveRecord error undefined "local variable or method" for belongs_to association with different foreign_key

Hi folks, big problem u = User.username_like('hans').first # works u.projects # error undefined "local variable or method" for project What's wrong with that? Model Code class User has_many :projects end Class Project belongs_to :owner, :class_name => "User", :foreign_key => "user_id" end please help, thanks in advance :) ...

Find record, that has ALL associated records

Say, we have a "Person" and "Favorite" models. "Favorite" is what this person likes: "music", "video", "sport", "internet", "traveling" etc. "Person" HABTM "Favorites", and "Favorite" HABTM "Persons" I need to find a Person, that has ALL listed "Favorites. For example, find a person, that likes "music", "traveling" and "sport". How ...

MySQL Active Record problem

I'm using MySQL ActiveRecord with CodeIgniter to submit queries, but have run into some snags when using the "select" function. For instance, this MySQL query works perfectly within phpMyAdmin: SELECT review_id, firms_name FROM reviews AS r JOIN firms AS f ON f.firms_id = r.review_target_id WHERE r.review_id =3 ORDER BY r.review_timest...

Benchmarking rails ActiveRecord Queries

I'm looking to benchmark a couple of my ActiveRecord requests in my app. What's the simplest way in the console to benchmark something like User.find_by_name("Joe").id versus User.find(:first, :select => :id, :conditions => ["name = ?","Joe"]).id Thanks ...

Rails - Multiple Index Key Association

There seem to be a number of ways to handle a multiple foreign key association. Each way I have approached this has their draw backs, and as I am new to Rails I am convinced others have come across a similar scenario and I am probably working on something solved long ago. My question is: What would be an efficient way of handling a mu...

Hibernate-like Criteria API for Ruby on Rails

Is there any gem that enables you to use something like the Criteria API from the Java persistence framework Hibernate? I think Hibernate Criteria API is one of the bests APIs ever for queries and I really miss it when developing in Ruby on Rails. I really don't like the way Raills work with ActiveRecord for queries. ...

RoR: Too many queries

Hi, My problem is the following: I have the models events and music_types, connect via hmabtm join table. If I call now MusicType.all in my event controller I get a complicated query for each music_type I have. Similar to: SELECT music_types.id FROM music_types INNER JOIN join_events_music_types ON music_types.id = join_event...

Ruby: How to cast from a class to another using ActiveRecord

Hi, I'm currently using activerecord STI and trying to cast an object type from A to B (same parent). Is there a standard way of achieving that? ...

activerecord equivalent to SQL 'minus'

What's the rails way to subtract a query result from another? A database specific SQL example would be: SELECT Date FROM Store_Information MINUS SELECT Date FROM Internet_Sales ...

Work with relations in Subsonic

Hi, I am using subsonic 3.0.3(ActiveRecords) in my winforms project. I am trying very hard to use Collections for my entities but so far I have been unable to do that . Please allow me to make myself more clear. While going across many tutorials, I have seen, that Subsonic will generate two classes. One is a collection class and the...

Add further information to Rails Select list?

Hi Everyone, I have a select list drop down in my Rails application, in the form for creating a new record. <li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li> This is so the user can choose from a list of Surveyor's ...