activerecord

Deriving a resource from the controller name - plugins?

Just wondering if there is a plugin out there that abstracts the process of deriving the instance of a current resource (or its class) from the current controller name? Currently I just classify.constantize the controller name, and if that works then I test for the id paramater and load the record if it exists. Yea, maybe it's a bit we...

How do I make a Rails ActiveRecord dependent on an attribute?

I have created an ActiveRecord for a customer, and now they would like it so when it's destroyed, it is actually kept around for a manual rollback. What I would like to do is create a boolean attribute called 'active', and it defaults to 1. When a record is destroyed, the attribute is toggled to 0. I know I could change all my queries...

How to select where ID in Array Rails ActiveRecord without exception

When I have array of ids, like ids = [2,3,5] and I perform Comment.find(ids) everything works fine. But when there is id that doesn't exist, I get an exception. This occurs generaly when I get list of IDs that match some filter and than I do something like current_user.comments.find(ids) This time I may have a valid comment ID, ...

rails join table problem

Part of my RoR application is responsible for managing web site designs portfolio. One web site design can have many images associated with it. One image can be associated only with one design. I use has_many statement with :through parameter to connect images with design through join table. And when image is deleted associated entry ...

Rails: Elegant way to structure models into subfolders without creating submodules

I have numerous models in my app/models folder. I'd like to clean this folder up a little bit. Move models that belong to each other in subfolders. The problem is that by convention the model class is namespaced into an according module. E.g. app/models/blog/post.rb app/models/blog/comment.rb app/models/user.rb so that: app/models/b...

Subsonic3 Active Record Query using SubSonic.Query.Select throwing errors

So I am trying to get the following query to work using SubSonic3 ActiveRecord with a Sql Server 2008 backend facilities = new Select().From<Facility>() .ExecuteTypedList<Facility>(); as well as the following facilities = new Select().From<Facility>() .InnerJoin<mem_Users_Facility>().InnerJoin<mem_User>()...

ActiveRecord table inheritence using set_table_names

Hi, I'm using ActiveRecord in Ruby on Rails. I have a table named documents(Document class) and I want to have another table data_documents(DataDocument) class which is effectively the same except for having different table name. In other words, I want two tables with the same behavior except for table name. class DataDocument < Docum...

Is there any gem that can help Activerecord Association attributes mapping ?

The gem can help like this : class Book belongs_to :author end book = Book.first book.author_name #=> same as book.author.name book.provide( :author_name, :title ) #=> will automatically map like this: { :title => book.title, :author_name => book.author.name } Is there any gem help ?? Thanks! ...

What to do when find doesn't find any records in Ruby on Rails

I am trying to accomplish the atypical library learning a language application. I have been able to create Books, People, and BookCheckOuts. I've got the relationships working pretty well but am having issues with wrapping my head around how to handle books that have never been checked out. I've created two properties on my book class C...

Using RoR with a legacy table that uses E-A-V

I'm needing to connect to a legacy database and pull a subset of data from a table that uses the entity-attribute-value model to store a contact's information. The table looks like the following: subscriberid fieldid data 1 2 Jack 1 3 Sparrow 2 2 Dan 2 ...

Difficult to use ActiveMerchant in a project that does not use ActiveRecord as its ORM?

ActiveMerchant seems to be baked with AR in mind. I've come to this conclusion based on the fact that the purchase() method returns an ActiveRecord::Billing::Response object: Is this correct and, if so, does this mean it might be difficult to use ActiveMerchant in a project that uses a different Ruby ORM (Sequel/Datamapper)? ...

Want to learn ActiveRecord pattern in C#

Could someone possibly provide code to a very simple activerecord class that I could use to learn with? Looking at the source on projects like Castle and SubSonic is a bit overwhelming. I'd like to learn the active record pattern to get an idea of how it works. I know whatever I build would be nothing compared to Castle or SubSonic, but ...

Rails - Problem calling column from join table

I have an index of users where I display the user name, location of user and a field called categories, which can range from zero to many items. I'm having trouble getting the categories to display properly. Here are my details: User has_many :user_categories has_many :categories, :through => :user_categories Categories has_many ...

Why are associated objects through a belongs_to not equal to themselves?

I have two classes with a has_many and belongs_to association: class Employee < ActiveRecord::Base has_many :contracts end class Contract < ActiveRecord::Base belongs_to :employee end I expect that the employee returned by the #employee method of the Contract class would be equal to itself, which means that the following unit test...

searching for and ranking results

I'm trying to write a relatively simple algorithm to search for a string on several attributes Given some data: Some data: 1: name: 'Josh', location: 'los angeles' 2: name: 'Josh', location: 'york' search string: "josh york" The results should be [2, 1] because that query string hits the 2nd record twice, and the 1st record once. ...

How to model the concept of "Featuring" (i.e., when an artist is "featured" on a song)

Sometimes more than one artist will be on a song. For example, Jay-z's new song "A Star is Born" features the artist Cole, and thus would be listed as "Jay-z (featuring Cole) - A Star is Born" in a catalog. My question is how to model this in my database. Right now it's pretty simple: every song belongs_to :artist and every artist has_m...

NHibernate or FluentNHibernate or ActiveRecord?

I am in a stage of mapping my CSharp classes into database tables. I have decided to use NHibernate as my ORM tool after comparing with other tools. I have never done a real project with NHibernate before and now am considering alternatives for the mapping, ActiveRecord: according to the project's web site, using ActiveRecord can boost...

Activerecord, 'foregin_key' has to be a combination of 2 fields

I want a has_many relationship described below class User < ActiveRecord::Base has_many :mcollections, :foreign_key=>'obj_id' end Below is definition of table mcollections create table mcollections ( id int not null auto_increment, obj_id varchar(255) not null, category ...

How do I make named_scope work properly with a joined table?

Here's my situation. I have two tables: pledges and pledge_transactions. When a user makes a pledge, he has only a row in the pledges table. Later when it comes time to fulfill the pledge, each payment is logged in my pledge_transactions table. I need to be able to query all open pledges which means that the sum of the amounts in the t...

ActiveRecord: Clever Join and Include

Hi all, I am trying to get ActiveRecord to perform the following query: SELECT A.*, B.*, C.* FROM A INNER JOIN B ON B.ID = B_ID INNER JOIN C ON C.ID = C_ID The dataset is rather large, and I need as the best performance, hence this specific query. I have my models and query as follows: class A < ActiveRecord::Base belongs_to :b ...