activerecord

Eager loading not working in Rails 2.2.2

I'm working with models analogous to the following: class Owner < ActiveRecord::Base has_many :owned end class Owned < ActiveRecord::Base belongs_to :owner end You can presume that owned_id and owner_id are in the right places. The trouble is that, in a controller for a different mvc chain in the app, @owner = Owned.find_by_id(...

How can I find out all the references for the current project and tell which ones derive from a particular base class in C#?

Due to politics at the very large finanial institution for which I work, I am not able to use Castle Project's ActiveRecord implementation, but like the pattern so much I have implemented it myself. This is complete, and now management is looking for a GUI tool to browse all the activerecord classes, search for instances and manage data....

Easy way to see SQL generated by ActiveRecord.Save()?

SubSonic 2.1 I have an ActiveRecord object. When I call its Save() method, is there an easy way for me to seen the generated SQL, say in the Visual Studio debugger? Running SQL Profiler is not an option. Thanks. ...

dynamic table names for Active Record models

I have an interesting Active Record problem and I'm not quite sure what the cleanest solution is. The legacy database that I am integrating with has a strange wrinkle in its schema where one logical table has been 'partitioned' into several physical tables. Each table has the same structure, but contains data about different items. I'm ...

Selecting only one row from child model based upon the parent model

Following is the association between 2 models: class FotoGossip < ActiveRecord::Base has_many :uploads end class Upload < ActiveRecord::Base belongs_to :foto_gossip end @latest_uploads = Upload.all(:include => :foto_gossip, :order => "created_at DESC", :limit => 5) It displays the latest 5 photos from Upload model. But, I want...

sort list of objects by property on associated object

Can I sort a list of objects by a property of an associated object? For example with the following class class RosterSlot < ActiveRecord::Base belongs_to :event belongs_to :skill belongs_to :person end I want to do something like RosterSlot.find(:all, :order => skill.name) which means activerecord needs to do a join and or...

For an ActiveRecord one-to-many association, which :foreign_key option should I set?

ActiveRecord's has_many and belongs_to methods both take a :foreign_key option. If I need to use it to handle a nonstandard FK column name, should I set it for the parent model (has_many), child model (belongs_to), or both, or does it matter? ...

Is there a way to avoid automatically updating Rails timestamp fields?

If you have DB columns created_at and updated_at Rails will automatically set those values when you create and update a model object. Is there a way to save the model without touching those columns? I am bringing in some legacy data and I would like to set those values from the corresponding values in the (differently named) legacy data...

global methods in ruby on rails models

Hi All, I have methods in all of my models that look like this: def formatted_start_date start_date ? start_date.to_s(:date) : nil end I would like to have something that automatically writes a method like this for each datetime field in each model, what's the best way to do this? -C ...

Rails/ActiveRecord: save changes to a model's associated collections

Do I have to save modifications to individual items in a collection for a model, or is there a method I can call to save them when I save the model. #save doesn't seem to do it. For example: irb> rental = #... #=> #<Rental id: 18737, customer_id: 61, dvd_id: 3252, date_rented: "2008-12-16 05:00:00", date_shipped: "2008-12-16 05:00:00"...

ActiveRecord and SELECT AS SQL statements

Hi all, I am developing in Rails an app where I would like to rank a list of users based on their current points. The table looks like this: user_id:string, points:integer. Since I can't figure out how to do this "The Rails Way", I've written the following SQL code: self.find_by_sql ['SELECT t1.user_id, t1.points, COUNT(t2.points) as u...

How do I use ActiveResource with a custom URL scheme?

I'm trying to create ActiveResource objects for three objects in an internal application. There are Tags, Taggings, and Taggables: http://tagservice/tags/:tag http://tagservice/taggings/:id http://tagservice/taggables/:type/:key Tag's :tag is the URL-encoded literal tag text. Tagging's :id is an autoincremented integer. Taggable's ...

Create an ActiveRecord database table with no :id column?

Is it possible for me to create and use a database table that contains no :id column in ActiveRecord, Ruby on Rails. I don't merely want to ignore the id column, but I wish it to be absolutely non-existent. Table Example :key_column :value_column 0cc175b9c0f1b6a831c399e269772661 0cc175b9c0f1b6a831c399e26977...

Ruby on Rails, ActiveRecord, Binary search.

If I had the following table. create_table :my_table, :id => false do |t| t.string :key_column t.string :value_column end How would I ensure that the rows are optimaly stored for binary search by the field of :key? And how would I make sure binary search is used? ...

Do rails rake tasks provide access to ActiveRecord models?

I am trying to create a custom rake task, but it seems I dont have access to my models. I thought this was something implicitly included with rails task. I have the following code in lib/tasks/test.rake: namespace :test do task :new_task do puts Parent.all.inspect end end And here is what my parent model looks like: class Pa...

ActiveRecord models cached in rake tasks?

I know that in rails 2.3.2 ActiveRecord queries are cached, i.e. you may see something in the development/production log: CACHE (0.0ms) SELECT * FROM `users` WHERE `users`.`id` = 1 I was wondering if the same principles apply to rake tasks. I have a rake task that will query a lot of different models, and I want to know if I should...

ActiveRecord field normalization

I feel bad asking this question, as I thought I knew enough about Activerecord to answer this myslef. But such is the way of having SO available ... I'm trying to remove the commas from a field in a model of mine, I want the user to be able to type a number , ie 10,000 and that number be stored in the database as 10000. I was hoping th...

rake db:schema:dump doesn't provide data migration info - is there an automated way to get this?

rake db:schema:dump This command gives you the schema of a legacy database and you can build a migration for that database off the generated schema. But if that database had data in it, it would be nice if there was a rake command to retrieve the data in a migration file generated by Rails. Perhaps I'm dreaming - it's probably aski...

Overwriting/Adding an ActiveRecord association dynamically using a singleton class

The business logic is this: Users are in a Boat through a join table, I guess let's call that model a Ticket. But when a User instance wants to check who else is on the boat, there's a condition that asks if that user has permission see everyone on the Boat, or just certain people on the Boat. If a User can see everyone, the normal dea...

ActiveRecord#exists? or rescue from ActiveRecord::RecordNotFound

What is the recommended way of handling the following type of situations: Supposing I have a model called Cart, that has a 1-1 relationship with the model Person and the same PK (the user's id). In the index method of my cart_controller I want to check if a Cart exists for the current user. If I do Cart.find(the_user_id) and a cart does...