我一直在争取这一点,现在我想创建一个左连接条件,所以我有一个优惠券模型,用户可以创建优惠券,但他们可能会或可能不会被分配给一份工作,当他们被分配时,他们被视为被执行。我使用add_reference :jobs, :coupon, index: true
索引在优惠券模型上设置为has_one :job
,但这看起来像是borked。我认为我的大脑今天被炒了...如果使用优惠券,理想情况下我想确认它被分配到有效的工作,这就是为什么我正在使用索引。导轨4模型关联左连接验证ID
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
和模型类...
class Coupon < ActiveRecord::Base
has_one :job
end
class Job < ActiveRecord::Base
belongs_to :coupon
end
这个问题不清楚。例如,你有没有任何理由在标题中提到了LEFT JOIN?什么想要验证? – Meier
@Meier发布的答案更清晰,我试图验证优惠券何时创建,稍后确保将其分配给有效的作业ID。我的后续思路在下面发布的答案? –