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.

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.

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 a better solution.

share|improve this question

1 Answer 1

 class EmailNotification < ActiveRecord::Base
  belongs_to :notification
  belongs_to :setting

class Notification < ActiveRecord::Base
  has_many :settings, :through => :email_notifications
  has_many :email_notifications
end


class Setting < ActiveRecord::Base
  has_many :notifications, :through => :email_notifications
  has_many :email_notifications
  validates_uniqueness_of :truck_fleet_id 
  # validates :email, :email_format => true
end

############## MEANWHILE IN CONTROLLERS #######
class SettingsController < ApplicationController
   def update
     # For example
     @setting = Setting.new setting_params
     @setting.save
   end
 # This is new feature of rails to sanitize the params, it only returns the permitted attributes to be updated.
 def setting_params
    params.require(:setting).permit(:email_periods, :email_recepient_id, :truck_identification, :truck_fleet_id)
 end
end

class NotificationController < ApplicationController
 # This is new feature of rails to sanitize the params, it only returns the permitted attributes to be updated.
 def notification_params
    params.require(:notification).permit(:name)
 end
end

class EmailNotificationController < ApplicationController
 # This is new feature of rails to sanitize the params, it only returns the permitted attributes to be updated.
 def email_notification_params
    params.require(:emailNotification).permit(:notification_id, :setting_id, :primary, :secondary)
 end
end
share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.