activerecord

Joins across multiple tables with ActiveRecord with named scopes

I love making named scopes for rails. however, I ran into somewhat of a pickle. Ive gotten pretty comfortable using named scopes for joins like so: named_scope :foo, :joins => :bar, :conditions => "bar_attribute = 'something'" Now pretend I have a table called baz which is contains a foreign key from the bar table. I need something l...

active record: create record from parent object...

this is what I mean: job has many docs. I want to create a doc, I can do: @doc = Doc.new(params[:doc]) but I'd like to enforce the parent-child relationship, since I already know the job.. something like this: @job.docs.new(params[:doc]) so that the job_id field gets ignored and only the @job object matters... does it make any se...

Should I use ON DELETE CASCADE, :dependent => :destroy, or both?

In a Rails app, I have foreign key constraints in MySQL which I set them all up manually, separate from my migrations. I'm trying to figure out whether I should use ActiveRecord's :dependent => :destroy option. For example, in my schema I have tables... users ----- log_entries ----------- user_id # Has FK constraint to users.id with ...

Problems with repository pattern reuse and query support

Guys, I've been reading both ddd and PoEAA books, and searching a lot on the web but i'm still confused and i think there is something i'm missing about the repository pattern: Context of use: Web apps (monorail + activerecord) What i'm looking for: reuse a 'base' repository among project. The repository will be the typical IReposit...

Rails ContactForm with non ActiveRecord model - validations

Can anyone share a few guides, tips or links to what constitute best practices for creating a model that represents a contact form which is not backed up by a table in the database, but can still validate the data entered? Thanks. EDIT: I forgot to mention, that if it is a gem or a plug-in, it should work with Ruby 1.9.1 on Rails 2.3.2...

What's the standard for percentages in Active Record?

I've been looking all over, and really just need a definitive answer to ride on. I'm going to have an object that contains percentages compiled from other objects, the point here is, what's the standard for representing a percentage in rails, without it getting broken in data migrations and things like that. ...

Delete everything from all tables (in Activerecord)

So I can do Post.delete_all to delete all my posts, but what if I want to delete all posts, comments, blogs, etc. I.e., how do I iterate over all my models and run the delete_all method? ...

Correct datatype for latitude and longitude? (in activerecord)

Should I store latitude and longitude as strings or floats (or something else)? (I'm using activerecord / ruby on rails, if that matters). ...

Immutable Active Record Relationships and Attributes (Setters)

I set out to solve a problem we have, where under certain circumstances; we may need to make an attribute or relationship immutable, that is once the attribute is written it is written forever. attr_readonly wasn't appropriate, as that didn't work for relationships, so I tried to prove the concept here: http://pastie.org/562329 This sh...

Using ActiveRecord belongs_to with two keys

Hi, I have two ActiveRecord models with a hasMany / belongsTo association: class User < ActiveRecord::Base has_many :letters end class Letter < ActiveRecord::Base belongs_to :user end The User model has a revision_number attribute, to which I would like to scope the belongs_to association, so the letter is associated to a User b...

Double join with habtm in ActiveRecord

I have a weird situation involving the need of a double inner join. I have tried the query I need, I just don't know how to make rails do it. The Data Account (has_many :sites) Site (habtm :users, belongs_to :account) User (habtm :sites) Ignore that they are habtm or whatever, I can make them habtm or has_many :through. I want to b...

Limit the results of a active record find

I've got a table with employees (id, name, role) and a relations table bosses (employee_id, superior_id; both foreign_keys to employees.id to employees). Now if a employee logs in, I only want to show his/her employees; an admin (role=admin) can see all employees. For the admin it's easy: Employee.find(:all) #to list them Employee.fin...

How can Rails' Activerecord table refer to itself?

Hello, everyone. I have an idea to use Activerecord to implement something strange like the example below: SystemInfo < ActiveRecord::Base belongs_to :SystemInfo end The idea is, System A can contain System B as its child. So I will generate application's skeleton as: script/generate scaffold SystemInfo parent_id:integer name:str...

has_one relationship validation in rails

Since has_one doesn't provide a before_add callback to allow validation, how do I prevent rails from destroying the old association even when the new one does'nt pass validation? susan :has_one :shirt shirt :belongs_to :susan susan.shirt = a_nice_shirt this destroys whatever association was present beforehand, even if the new shirt is...

Rails :include vs. :joins

This is more of a "why do things work this way" question rather than a "I don't know how to do this" question... So the gospel on pulling associated records that you know you're going to use is to use :include because you'll get a join and avoid a whole bunch of extra queries: Post.all(:include => :comments) However when you look at ...

Running ActiveRecord validations without saving

I have a collection of ActiveRecord objects. I want to be able to run all the validations for each of these objects without actually saving them to the database. I just want to know if they would be valid were I to save them to the database. In other words, I essentially want to populate the errors data structure for each of my objects...

Rails Rake MySql Autoincrement Problem w db:fixtures:load

I'm trying to load some Rake Fixtures (rake db:fixtures:load) into a MySql database and I'm seeing some weird behaviour with AutoIncrement values. Normally this goes up by 1 for each insert which allows me to define/create tests. (BTW - normal create/insert from script works correctly). However when I load from fixtures the id field is ...

How do I use ValidateDecimal attribute in Castle.ActiveRecord?

I have a class that inherits ActiveRecordValidationBase that contains the following property: [Property] [ValidateDecimal] public Decimal UnitCost { get; set; } I also have a UnitCostTextBox that accepts input for said UnitCost. What I would like to do is perform validation once using Castle's validators. However, it seems that befo...

belongs_to not using primary key option

Hello, I've been struggling with this for a while, and decided to throw it out there: I have 3 models, User, Connection, Suspect A User has many Connections, A Connection has one Suspect, linked via case_id A User has many Suspects through its Connections. The code is as follows: class User < ActiveRecord::Base has_many :followe...

Ruby on rails model with multiple parents

In my Rails application, I have two models, Articles and Projects, which are both associated with a user. I want to add comments to each of these models. What's the best way to structure this? Here's my current setup: class Comment < ActiveRecord::Base belongs_to :article belongs_to :project end class Article < ActiveRecord::Base ...