activerecord

How to verify that the assoication is valid

I have a model called Profile which is belong_to User, so there is 'user_id' for the database to keep track of. In the local admin interface I made for this model I wanted to provide the flexibility of allowing admin to enter an username to a field in the editing screen, and then resolve that to user_id for saving in controller. However...

Many-to-Many Association To Same Model with ActiveRecord?

I'm following this tutorial which is working splendidly for has_many :through relationships. I've got normal things like category_product working. However, I am not able to conceptualize this situation (nor get it to work): I have a Category that has related Categories. Since every category can have N categories... first off, is this ac...

What is the difference between a ORM, AR, QB, & DM?

Ok, so everyone has decided (and for good reason) strait SQL is of the devil. This leaves us with many methods of placing a "middle-man" in our code to separate our code from the database. I am now going to spit out all the info I have gathered in the hope someone can set me strait and tell me what I built. An ORM (Object-relational map...

Does the DataMapper pattern break MVC?

I have been reading up on multiple PHP frameworks, especially the Zend Framework but I am getting confused about the proper way to go forward. Zend Framework does not use ActiveRecords but instead uses the Table Data Gateway and Row Data Gateway pattern, and uses a DataMapper to map the contents of the Row Data Gateway to the model, bec...

NHibernate, ActiveRecord, Transaction database locks and when Commits are flushed

This is a common question, but the explanations found so far and observed behaviour are some way apart. We have want the following nHbernate strategy in our MVC website: A SessionScope for the request (to track changes) A ActiveRecord.TransacitonScope to wrap our inserts only (to enable rollback/commit of batch) Selects to be outside ...

How do I model this multi-inheritance relationship w/ Ruby ActiveRecord?

Assuming I have 5 tables. Can ActiveRecord handle this? How would you set it up? The hierarchy: Account (Abstract) CorporateCustomer (Abstract) PrivateCustomer PublicCustomer GovernmentCustomer Edit: In nhibernate and castle activerecord the method needed to enable this scenario is called "joined-subclasses". ...

Joins Using AS in :select with ActiveRecord

I am trying to do this Version.find(:all, :joins=>"JOIN editions ON versions.edition_id=editions.id JOIN products ON editions.product_id=products.id", :select=>"products.name, versions.name AS what") but ActiveRecord is not respecting the AS keyword... any ideas? Edit: Since both fields are called "name" they are colliding, so I only...

Best practices for getting a list of IDs from an ActiveRecord model

I have an ActiveRecord model Language, with columns id and short_code (there are other columns, but they are not relevant to this question). I want to create a method that will be given a list of short codes, and return a list of IDs. I do not care about associations, I just need to end up with an array that looks like [1, 2, 3, ...]. M...

Rails find :include in a multi DB environment

Hey guys, my first question here. I am making a site in RoR and I am inside a multi DB environement. By that, I mean that some of my models are linked to MSSQL tables, and some others are linked to MYSQL tables. It works well for the most part, but when I use the 'include' option in a find method, I get a very weird mix of SQL. Let me ...

Modelling teams

Hello, I just wanted to get some feedback on better ways to model a team/team membership in rails. I currently have the following: class User has_many :teams, :foreign_key => "owner_id" #user owns this team has_many :memberships #user is a member of these teams class Team belongs_to :team_administrator, :class_name => "Use...

Complex rails query - unions? sub-select? can I still used named_scope?

Part of why I love Rails is that I hate SQL - I think it's more like an assembly language that should be manipulated with higher level tools such as ActiveRecord. I seem to have hit the limits of this approach, however, and I'm out of my depth with the SQL. I have a complex model with lots of sub-records. I also have a set 30-40 named...

Find all objects that have an associated object with a certain property

I have an Order class that has_many :shipments. How can I use Order.find to return all the order objects whose newest shipment was created after a certain time (say, in the last hour)? ...

Why does form_helper assume that it should call active_record_helper?

This code in my view triggers an error: <% remote_form_for(mymodel) do |f| %> <%= f.error_messages %> (mymodel is not an ActiveRecord object) When I look at the error trace, I see this section which indicates that error_messages in the view translates into error_messages_for in the active_record_helper: C:/Ruby18/lib/ruby/gems/1.8...

How does this deprecate method work?

I was trying to understand this call: deprecate :new_record?, :new? which uses this deprecate method: def deprecate(old_method, new_method) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{old_method}(*args, &block) warn "\#{self.class}##{old_method} is deprecated," + "use \#{self.class}##{...

Rails: Virtual attributes and form values

Hi. I have a Model Book with a virtual attribute for create a Editor from the Book form. The code looks like: class Book < ActiveRecord::Base has_many :book_under_tags has_many :tags, :through => :book_under_tags has_one :editorial has_many :written_by has_many :authors, :through => :written_by def editorial_string self...

Update parent Model on child update

I have two scaffold-generated models: Book and Bookbag. A Bookbag has-many Books, and a Book belongs-to a Bookbag. Each Book has a weight, and each Bookbag has an average-weight that is supposed to store the average weight of all of its Books. What is the best way to keep average-weight up to date? Using a before-save filter on Bookba...

ActiveRecord: Avoid inconsistency in has_many relations

Assume we have an usual M-M relationship between two tables, like: users---< users_tags >--- tags. In this post I'm only concerned about the relation user_tags, tags: I'd like to avoid that linked tags can be deleted. Only tags which aren't referenced should be destroyable. The stupid way to do this would be: class Tag def before...

Can you do an :order in ruby after a find?

Is it possible in ruby/rails to order a result set after doing a find? For example, is it possible to do something like this Warning: Does not work if type == 1 @items = mycompany.items else @items = myhome.items end @items = @items :order => "created_at" I would assume something like this should be possible, but I'm still very...

Rails Nested resource access problem in resource_controller

I using the Rails resource_controller plugin. I have a ReleasesController, which is nested within a UsersController and a ProjectsController. Resource_controller fails when it attempts to pull the release from User, but succeeds from Project. The problem under User is finding the Release results in an object of type, Enumerable::Enumera...

How do I do reflexive self-join relationships in ActiveRecord?

I'm trying to implement a social networking style friendship model and I didnt have much much luck trying to figure out the plugins available out there. I think I'll learn Rails better if I do it myself. So here's what I have : class User < ActiveRecord::Base has_many :invitee_friendships , :foreign_key => :friend_id, ...