activerecord

find a random entry from an association in rails

Controller: class GalleriesController < ApplicationController def index @galleries = Gallery.all end end View: <% for gallery in @galleries %> <%= image_tag(gallery.image.url(:medium)) %> <% end %> I have 2 models, Photo which belongs to Gallery,which has many Photos. I want to display an image (preferably random rather t...

ActiveRecord query assistance

Hey all, i need a little help with a AR query. This is how my models look like: class User < AR:B has_many :publications end class Publication < AR:B belongs_to :user belongs_to :category end class Category < AR:B has_many :publications end Now let's say I want to iterate over all existing categories and either display the ...

Rails 2 :joins and :include resultset

When fetching content from a database using activerecord, I would like to fetch a custom resultset with specified columns across two tables. SELECT users.name, users.username, users.age, users.city_id, cities.name as city_name FROM users INNER JOIN cities ON users.city_id = cities.id Which would be in AR as Users.find(:all, :join...

ActiveRecord Table Aliases

Does anyone know if it's somehow possible to setup an alias for an ActiveRecord table join? Something like: User.find(:all, :alias => "Users as u", :joins => "Friends as f", :select => "u.id,f.name") Any ideas? ...

What is the class of an association based on the foreign key attribute only?

Short: I have an foreign key attribute and want to know what is the class (or reference table) of that foreign key field. Context: given 2 tables: users(id, [other fields]) and issues(id, user_id, assigned_to, [other fields]) Here is my active record of Issue (irrelevant parts are extracted) class User < ActiveRecord::Base ... end ...

SubSonic 3 - Retrieve values from dirty columns.

Hi, I am using SubSonic 3.0.0.4 with the ActiveRecord T4 Templates. I am hooking into OnSaving and getting a list of dirty columns that are about to be saved. What I cannot figure out is how to get each of the dirty columns values. Can someone assist? TIA - Mike ...

How to determine the time taken to complete a Rails query

I would like to include query statistics (i.e. records returned and elapsed time) in my Index views (similar to Google's). I found the records returned, but am having difficultly locating the elapsed time. I'm using Rails3 beta and the will_paginate plugin. ...

Import - csv to activerecord

I'm writing an import routine that will allow a user to upload a CSV file to load their database. Each row of the CSV corresponds to a model. I'm using FasterCSV to read the file and split the data into individual models, which is working great. I'm just having trouble deciding on the best approach to handle errors. Right now I have t...

Rails 2 options_for_select with an ActiveRecord resultset

Does anyone know a better way of displaying the contents of an activerecord result set as a select box. I would like todo this @users = User.all <%= f.select_box :users, options_for_select(@users) %> But for now I need to parse all the users into a multidimensional array with a sub array [username,user_id] Any ideas? ...

How do I write a named scope to filter by all of an array passed in, and not just by matching one element (using IN)

I have two models, Project and Category, which have a many-to-many relationship between them. The Project model is very simple: class Project < ActiveRecord::Base has_and_belongs_to_many :categories scope :in_categories, lambda { |categories| joins(:categories). where("categories.id in (?)", categories.collect(&:to_i)) } ...

LEFT OUTER joins in Rails 3

I have the following code: @posts = Post.joins(:user).joins(:blog).select which is meant to find all posts and return them and the associated users and blogs. However, users are optional which means that the INNER JOIN that :joins generates is not returning lots of records. How do I use this to generate a LEFT OUTER join instead? ...

avoid loop in returning the same attribute in the definition of the method

Hello all, I have a simple article model with a predefined_title attribute and a user_defined_title attribute All I want is to make a virtual attribute that shows the user_defined_title if available and predefined_title if not But I thought what a waste to add another virtual attribute, if I could only do something like this def user_...

Efficient way to implement this ActiveRecord Query

Hi I am using MySQL database and I have a datetime field inside it. In my rails application I have to fire a Query similar to the following, MyTable.all(:conditions=>{my_date.to_date=>"2010-07-14"}) The my_date field is of datatype datetime. I should omit the time and should directly compare it with a date (but I am encountering an e...

LINQ Query leaves all items with _IsTrue = true

Hi All, I have the following simple LINQ query (SubSonic 3.0.0.4 and ActiveRecord T4 templates). from item in PermissionsInRole.All() where item.RoleID == roleID select item When I iterate through the elements in the list after executing the query, IsNew = true for all of them, so if I make a change to an item and call.Save() it tri...

Rails/Activerecord database field timezone

I have a database that is written to by a non-rails application and read from by a rails application. There is a datetime field in the database and the data stored in this field is stored in Eastern Time. In my rails app, I understand I can set the application's timezone in my environment.rb file by doing config.time_zone = 'Eastern Time...

Rails 3 Autosave Associated Records For Error

I have the following two models that have has_many and belongs_to relationships. I am trying to sort the ActionItems that belong to an ActionList using the reorder_action_items method. class ActionItem < ActiveRecord::Base belongs_to :action_list default_scope order("sort_order asc, created_at asc") end class ActionList < Active...

Eager loading for polymorphic associations

Hi, Not sure this could fall in performance section as well as model/database section, so here goes.... Let's say I have 3 models: Movie { has_one :interest, :as => :resource } Song { has_one :interest, :as => :resource } Story { has_one :interest, :as => :resource } and ... Interest { belongs_to :resource, :polymorphic => true } ...

ActiveRecord custom SQL in Ruby

I need to perform these SQL below, but I couldn't get the result. newid=Header.find_by_sql( "SELECT coalesce(max(transaction_id),0)+1 AS id FROM transaction_headers WHERE transaction_year = #{Time.now.year} AND transaction_type='#{fields[:transaction_type]}'" ) But I can't seem to get the result to newid. The only value I ...

Yii paginate related data

What is the best way to paginate related data in Yii? For example I may have post and I want to paginate comments. ...

Ruby on Rails/Activerecord mySQL modeling

This is a pretty simple question really, but let's say I'm creating a model for Person. Person obviously has first name, last name, age, etc. But the person also has contact info consisting of things like address line 1, address line 2, city, state, zip, country, phone 1, phone 2, etc... Does it make more sense to create a model for P...