Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Working on the Rails project where I have many-to-many :through relationship.

Quickly introduce you to the code base: each setting has many notifications through email_notification model where the notification can be primary or a secondary.

schema.rb

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"

In models

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 frequency, or interval etc.

Let me know if you can find better solution

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.