polymorphic-associations

Modeling a cellphone bill: should I use single-table inheritance or polymorphic associations?

In my domain: Users have many Bills Bills have many BillItems (and therefore Users have many BillItems through Bills) Every BillItem is one of: Call SMS (text message) MMS (multimedia message) Data Here are the properties of each individual BillItem (some are common): My question is whether I should model this arrangement with s...

accepts_nested_attributes

I'm trying to use a comment style model which is attached to another model but I keep getting the error: Review(#2171315060) expected, got Array(#2148226700) With params: Processing PlacesController#create (for 127.0.0.1 at 2010-04-15 18:57:02) [POST] Parameters: {"commit"=>"Submit", "action"=>"create", "destination_id"=>"3...

Refer to similar associated models with a common name

I have these models: class Bill < ActiveRecord::Base has_many :calls has_many :text_messages end class Call < ActiveRecord::Base belongs_to :bill end class TextMessage < ActiveRecord::Base belongs_to :bill end Now, in my domain calls and text messages are both "the same kind of thing" -- i.e., they're both "bill items". So I...

How to add a has_many association on all models

Right now I have an initializer that does this: ActiveRecord::Base.send :has_many, :notes, :as => :notable ActiveRecord::Base.send :accepts_nested_attributes_for, :notes It builds the association just fine, except when I load a view that uses it, the second load gives me: can't dup NilClass from: /usr/lib/ruby/gems/1.8/gems/activereco...

nested attributes with polymorphic has_one model

I am using accepts_nested_attributes_for with the has_one polymorphic model in rails 2.3.5 Following are the models and its associations: class Address < ActiveRecord::Base attr_accessible :city, :address1, :address2 belongs_to :addressable, :polymorphic => true validates_presence_of :address1, :address2, :city end class Vendor <...

Polymorphic associations in Rails

Say I have two models, Apples and Oranges, and they are both associated with a description in a Text model. Text is a separate class as I'd like to keep track of the different revisions. Is the following correct? Is there a better way to do this? [Apple] has_one :text, :as => :targit, :order => 'id DESC' has_many :revisions, ...

Why is my model firing the validation at the wrong time?

In my edit action of my employees_controller I have this line of code: #Employee#edit 69: if @employee.user.person.addresses.length == 0 70: @employee.user.person.addresses << Address.new 71: end which should add a blank Address if there are none so it will show up in my edit erb file. That way if there were no Addresses associated ...

How do I create a polymorphic model with a collection_select?

This are my models: class Speaker < ActiveRecord::Base belongs_to :session, :foreign_key => :session_id, :class_name => :Session belongs_to :speakable, :polymorphic => true end class Session < ActiveRecord::Base has_many :speakers accepts_nested_attributes_for :speakers end class Person < ActiveRecord::Base has_many :speak...

Ruby on Rails: How can I authenticate different user types from one place?

Hi everyone! This is my first post on Stack Overflow. I am trying to build a system that authenticates three types of user with completely different site experiences: Customers, Employers, and Vendors. I'm thinking of using a polymorphic 'User' table (using AuthLogic) with username, password, and user_type (+ AuthLogic's other required...

How do I check if an instance has an object to skip displaying the values?

I have created a polymorphic association around a model called status. Some contacts will have a status associated with it. Many won't. If I try to call a status when one is not there, I get an error. Right now, even if I haven't created a status for the model, it still runs whatever is in the if-end block. Here's what I am trying, ...

Rails preventing duplicates in polymorphic has_many :through associations

Is there an easy or at least elegant way to prevent duplicate entries in polymorphic has_many through associations? I've got two models, stories and links that can be tagged. I'm making a conscious decision to not use a plugin here. I want to actually understand everything that's going on and not be dependent on someone else's code that...

Associating Models with Polymorphic

I am trying to associate Contacts with Classes but as two different types. Current_classes and Interested_classes. I know I need to enable polymorphic but I am not sure as to where it needs to be enabled. This is what I have at the moment class CreateClasses < ActiveRecord::Migration def self.up create_table :classes do |t| ...

Rails: Polymorphic User Table a good idea with AuthLogic?

Hi everyone, I have a system where I need to login three user types: customers, companies, and vendors from one login form on the home page. I have created one User table that works according to AuthLogic's example app at http://github.com/binarylogic/authlogic_example. I have added a field called "User Type" that currently contains ei...

Database best practices

I have a table which stores comments, the comment can either come from another user, or another profile which are separate entities in this app. My original thinking was that the table would have both user_id and profile_id fields, so if a user submits a comment, it gives the user_id leaves the profile_id blank is this right, wrong, i...

polymorphic nested comments

Hi all i'm getting this error when I try to post a comment on a post. The error Cannot redirect to nil! /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:1104:in `redirect_to' /Users/Mister/dev/blog/app/controllers/comments_controller.rb:9 Comments/Controller: class CommentsController < ApplicationCon...

How to combine two models in one RoR acts_as_tree treeview?

I have two simple models each with acts_as_tree, say Departments and Employees. My goal is to create a treeview combining both models in to one overall tree, like so: Department 1 SubDepartment 1.1 Employee A Employee B SubDepartment 1.2 Department 2 Subdepartment 2.1 Employee C Department 3 SubDepartment 3.1 Employee D E...

How to model has_many with polymorphism?

I've run into a situation that I am not quite sure how to model. EDIT: The code below now represent a working solution. I am still interested in nicer looking solutions, though. Suppose I have a User class, and a user has many services. However, these services are quite different, for example a MailService and a BackupService, so singl...

jpa join query on a subclass

I have the following relationships in JPA (hibernate). Object X has two subclasses, Y and Z. Object A has a manyToOne relationship to object X. (Note, this is a one-sided relationship so object X cannot see object A). Now, I want to get the max value of a column in object A, but only where the relationship is of a specific subtype, ie...

join same rails models twice, eg people has_many clubs through membership AND people has_many clubs through committee

Models: * Person * Club Relationships * Membership * Committee People should be able to join a club (Membership) People should be able to be on the board of a club (Committee) For my application these involve vastly different features, so I would prefer not to use a flag to set (is_board_member) or similar. I find myself wanting ...

JPA/Hibernate Polymorphic Query with @Any

Hi there, I have a jpa mapping class with a polymorphic association like the following: @Entity class MyEnt { private Long id; private MyInterface mi1; private MyInterface mi2; //... @Any(metaColumn = @Column(name = "mi1_type")) @AnyMetaDef(idType = "long", metaType = "string", metaValues = { @MetaValue(valu...