You might have instances where the associations could be with multiple models. For example, if you have a Picture model that can be associated with either a Student model or an Employee model. In this case, you really want to be able to call @picture.imageable to get the image. This is where polymorphic associations come in.
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Student < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
You can think of a polymorphic belongs_to declaration as setting up an interface that any other model can use.
You can also retrieve a collection of pictures: @student.pictures or @employee.pictures from instances of Student or Employee model.
To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:
class CreatePictures < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
t.string :name
t.integer :imageable_id
t.string :imageable_type
t.timestamps
end
end
def self.down
drop_table :pictures
end
end
Finally, you can read more on the documentation written by Rails team.
You may be working on multi-master database model which required to use UUID and sometime because of using auto incrementing integer primary key just is not good enough.
So, you want to replace the default integer-based primary keys in your model with a UUID.
Now you need to disable the default …
I have presented about “Introduction to Refactoring” with Mr. Dane at BarCamp PP2. What interesting for me was “Coder Camp” which got more helps from Mr. Sokun to make it up without preparation, especially the room was full.
You can get more presentations from Share Vision Team. Thanks …
Recently, I install GeoServer 1.7.6 on Ubuntu 9.04. I would like to share some information with everybody what I have configured on my local PC.
As far as I follow the installation instruction of GeoServer documentation for Linux I get the error message “The JAVA_HOME environment variable is not …
If you have the algorithms with conditional statements in the client code it will be messy. The Strategy pattern moves an algorithm from the client code to a separate class. A program that requires a particular service or function and that has several ways of carrying out that function …
It can be an easy way to do if you have a database with schema changes. You have to convert a database to scripts so that you can commit to source control by comparing script files. Or you get update from source control as scripts and synchronize to your local …
Last two months I posted about Factory Method Design Pattern in C#. Now I do the same thing in JavaScript.
This is a simple example of the factory method pattern in JavaScript, The factory method getPhone() will generate Phone object. I just keep as organizing with namespace “Patterns” as in …
My colleague have some problems using Selenium to test the website specifically issues with JavaScript and Ajax. Also we would like to include testing nicely and easily integrated into our build process with continuous integration.
So we did together a bit of searching and found the free .Net automation framework …
The factory method pattern is a design pattern that allows for the creations of objects without specifying the type of object that is to be created in code. A factory class contain a method that allow determination of the created type at run-time. It is used to replace class …
Working everyday and sitting at your desk in front of your computer, you stare into space, trying to figure out how to write a new feature for your software. You know intuitively what must be done, what data and what objects come into play, but you have this underlying feeling …