I'm working on the Rails project where I have many-to-many :through relationship. Each setting has many notifications through email_notification model where the notification can be primary or a secondary.
create_table "email_notifications", :force => true do |t| t.integer "setting_id" t.integer "notification_id" t.boolean "primary" t.boolean "secondary" create_table "notifications", :force => true do |t| t.string "name" create_table "settings", :force => true do |t| t.string "truck_identification" t.string "email_periods" t.integer "email_recepient_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "truck_fleet_id"
class EmailNotification < ActiveRecord::Base attr_accessible :notification_id, :setting_id, :primary, :secondary belongs_to :notification belongs_to :setting class Notification < ActiveRecord::Base attr_accessible :name has_many :settings, :through => :email_notifications has_many :email_notifications end class Setting < ActiveRecord::Base # TODO: refactor this bullshit attr_accessible :email_periods, :email_recepient_id, :truck_identification, :truck_fleet_id has_many :notifications, :through => :email_notifications has_many :email_notifications validates_uniqueness_of :truck_fleet_id # validates :email, :email_format => true end
I used this many-to-many relationship because I need flexibility to add up new Notifications over the time, and keep ability to make notifications flexible as possible. For example, I might add a column
Let me know if you can find a better solution.