Reputation: 155
I'm having an issue where subscriber[:job_id]
is at first correct but is then getting set to 0
.
#controllers/subscribers_controller.rb
class SubscribersController < ApplicationController
def create
@subscriber = Subscriber.new(subscriber_params)
puts subscriber_params #THIS OUTPUTS {"job_id"=>"EtcaA1a1p00J", "email"=>"[email protected]"}
puts @subscriber #THIS OUTPUTS <Subscriber:0x007fa36c88c658>
puts @subscriber.job_id #THIS OUTPUTS 0
puts @subscriber.email #THIS OUTPUTS [email protected]
@job_id = subscriber_params[:job_id]
result = NewSubscriptionService.(subscriber: @subscriber, job_id: @job_id)
if result.success?
redirect_to result.job, success: 'You will be notified of changes to this job.'
else
redirect_to result.job, alert: 'Sorry, we were not able to subscribe you to this job.'
end
end
def subscriber_params
params.require(:subscriber).permit(:job_id, :email)
end
end
Which outputs:
#console output
app/controllers/subscribers_controller.rb:6:in `create'
Started POST "/subscribers/create" for 127.0.0.1 at 2018-05-16 10:28:08 -0600
Processing by SubscribersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zAj1bFJSrxkngxlybsib7/S7b+xAXouuXonujnFSELQkk3j3akG5rhnzK0g2URR/P3Z5v33KCIjOWiwi3A8F0w==", "subscriber"=>{"job_id"=>"EtcaA1a1p00J", "email"=>"[email protected]"}, "commit"=>"Watch this job"}
(0.2ms) BEGIN
SQL (1.0ms) INSERT INTO "subscribers" ("job_id", "email", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["job_id", 0], ["email", "[email protected]"], ["created_at", "2018-05-16 16:28:08.523325"], ["updated_at", "2018-05-16 16:28:08.523325"]]
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 63ms (ActiveRecord: 6.3ms)
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: insert or update on table "subscribers" violates foreign key constraint "fk_rails_2090f82b35"
DETAIL: Key (job_id)=(0) is not present in table "jobs".
: INSERT INTO "subscribers" ("job_id", "email", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"):
app/services/new_subscription_service.rb:14:in `perform'
app/services/new_subscription_service.rb:10:in `call'
app/controllers/subscribers_controller.rb:6:in `create'
The params are being passed to a service object:
#services/new_subscription_service.rb
class NewSubscriptionService
def initialize(params)
@subscriber = params[:subscriber]
puts params[:subscriber] #THIS OUTPUTS <Subscriber:0x007fa36c88c658>
puts params[:subscriber][:job_id] #THIS OUTPUTS 0
puts @subscriber.job_id #THIS OUTPUTS 0
@job_id = params[:job_id]
@job = Job.find_by(hash_id: @job_id)
end
def self.call(params)
new(params).perform
end
def perform
if subscriber.save
OpenStruct.new(success?: true, subscriber: subscriber, job: job, error: nil)
else
OpenStruct.new(success?: false, subscriber: subscriber, job: job, error: subscriber.errors)
end
end
private
attr_reader :job_id, :job, :subscriber
end
Am I doing something wrong in NewSubscriptionService
that is causing job_id
to change? email
survives every time, so I'm not sure what is happening to job_id
.
Thanks for the guidance.
Upvotes: 0
Views: 49
Reputation: 5609
When you call NewSubscriptionService.new
in your controller, you pass 2 parameters.
Just change your service's initialize
method with these params:
#controller
result = NewSubscriptionService.new(@subscriber, @job_id)
# service
def initialize(subscriber, job_id)
@subscriber = subscriber
@job_id = job_id
@job = Job.find_by(hash_id: @job_id)
end
edit : btw, if you want to keep a hash, then pass an hash and not 2 params
NewSubscriptionService.({ subscriber: @subscriber, job_id: @job_id })
Upvotes: 1