Last active
August 29, 2015 14:19
-
-
Save jcutrell/22a1da1ca068322ffb86 to your computer and use it in GitHub Desktop.
Weird behavior...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseJob < ActiveRecord::Base | |
belongs_to :child_job, polymorphic: true | |
has_attached_file :header_image, :styles => { :medium => "800x500#" } | |
validates_attachment_content_type :header_image, :content_type => /\Aimage\/.*\Z/ | |
validates :title, presence: true | |
validates :prompt_html, presence: true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkJob < ActiveRecord::Base | |
include BaseJobMethods | |
has_one :base_job, as: :child_job | |
has_many :job_completions, as: :job | |
belongs_to :redirector | |
accepts_nested_attributes_for :base_job | |
validates :base_job, presence: true | |
validates :redirector, presence: true | |
def completed_by_user?(user) | |
false if user.nil? | |
user.has_completed_job?(self) | |
end | |
def has_user_met_requirements?(user) | |
false if user.nil? | |
user.redirector_clicks.where(redirector_id: redirector.id).exists? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe LinkJob, type: :model do | |
it { should validate_presence_of :base_job } | |
it { should validate_presence_of :redirector } | |
it "should return true when asked if it is #persisted?" do | |
link_job = create(:link_job) | |
expect(LinkJob.count).to eq(1) | |
expect(link_job.redirector.persisted?).to eq(true) | |
expect(link_job.base_job.persisted?).to eq(true) | |
expect(link_job.new_record?).to eq(false) | |
expect(link_job.destroyed?).to eq(false) | |
expect(link_job.persisted?).to eq(true) # fails here... | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Redirector < ActiveRecord::Base | |
has_many :redirector_clicks | |
has_one :link_job | |
end |
(The thing that led me to this was the fact that the form submission caused what looked like a duplicate record, but in fact was due to the form having the new link_jobs path set on it, then I tracked it down to the persisted method by looking at the form_for helper itself to determine which path it drops into resource forms.)
As it turns out, the include BaseJobMethods
line referenced a module that included ActiveRecord::Base. Removing that inclusion fixed the issue.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This presented itself when I went to edit a link job, which is set up as a regular resource in rails. The form fields fill out properly. But the persisted? method doesn't return true, even though the link_job record has been persisted. This could be related to rails/rails#13744 - I haven't looked very far into this, as I'm expecting I'm doing something that is just a bad pattern and maybe we need to refactor our relationship between the base job and the link job, or something like that... Baffling, though, as that test passes all the way up to the final line.