I'm struggling a bit for this, I want to create a LEFT join condition, so I have a coupon model, users can create coupons, but they may or may not be assigned to the task, when they are assigned they are considered fulfilled. I had this setting as has_one :job on a coupon model with the index add_reference :jobs, :coupon, index: true , but it looks like borked. I think my brain is fried today ... If a coupon is used, I would ideally want to confirm that it is assigned to a valid job, so I worked with the index.
class CreateCoupons < ActiveRecord::Migration def change create_table :coupons do |t| t.string :code, limit: 250, null: false t.datetime :start_at, null: false t.datetime :end_at, null: false t.datetime :executed_at t.datetime :deleted_at t.timestamps null: false end add_reference :jobs, :coupon, index: true add_index :coupons, :deleted_at add_index :coupons, :code, unique: true end end class CreateJobs < ActiveRecord::Migration def change create_table :jobs do |t| t.string :title, limit: 50, null: false t.string :slug, limit: 250, null: false t.string :location, limit: 100, null: false t.string :short_description, limit: 250, null: false t.text :description, null: false t.text :application_process, null: false t.boolean :is_featured, default: false t.datetime :start_at t.datetime :end_at t.timestamps null: false end add_index :jobs, :slug end end
And model classes ...
class Coupon < ActiveRecord::Base has_one :job end class Job < ActiveRecord::Base belongs_to :coupon end
ruby ruby-on-rails activerecord model-view-controller
Chris Hough Mar 04 '16 at 22:55 2016-03-04 22:55
source share