You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Nothing is returned when using either first_or_create or find_by #13
I have found an issue with the first_or_create function. If the record doesn't exist, it is created. If the record does exist, it does not return it, but instead throws a Mailchimp::Exception::BadRequest. Is that what should happen? 🐙
Right, back to this now. I was wrong, it throws a Mailchimp::Exception::Duplicate, which is expected, but ideally a record should be returned, right? I see the return line here.
There are no errors if I use find_by, but nothing is returned.
I have previously gotten around it by manually going through the members list and checking if a record exists. The record is updated if it does, otherwise a new record is created...
defupdate_membermemberFound=falselist=MAILCHIMP.lists('0719f1b43e')list.members.find_eachdo |member|
ifmember.email_address == @email# Update the merge fieldsmember.updatemerge_fields: member_attributes[:merge_fields]# Update the interest groupmember.updateinterests: member_attributes[:interests]memberFound=trueendendifmemberFound == falseMAILCHIMP.lists('0719f1b43e').members.first_or_create(member_attributes)endend
No, Ruby returns the last thing that was evaluated in the method. It's normal Ruby style to not use return unless you want to return early from a method.
Arr I think I know why... What does the angle brackets mean? As it outputs as nothing ('nil') in the logs. I know there is something there because I have tried puts record.nil?, which returns 'false'.
If I run puts record in the rails console, I get...
<mail@ckhatton.com>
=> nil
If I run puts 'Account: ' + record in the rails console, I get...
TypeError: no implicit conversion of Mailchimp::List::Member into String
As the result is 'nil', I think that is why I don't see the output in the log.
The same happens for both find_by and first_or_create
What I cannot get my head around is why first_or_create throws Mailchimp::Exception::Duplicate when run via my controller, but not when run via the rails console (when the member already exists on the list).
Activity
dominicsayers commentedon Mar 7, 2017
It seems OK for me. I tried something like this:
Internally,
first_or_createcalls thefind_bymethod. Can you repeat your test usingfind_byand let me know if this also throws an exception?Could you also share some code to give more details of how you were using the
first_or_createmethod?ckhatton commentedon Mar 7, 2017
Right, back to this now. I was wrong, it throws a
Mailchimp::Exception::Duplicate, which is expected, but ideally a record should be returned, right? I see thereturnline here.There are no errors if I use
find_by, but nothing is returned.ckhatton commentedon Mar 7, 2017
I have previously gotten around it by manually going through the members list and checking if a record exists. The record is updated if it does, otherwise a new record is created...
[-].first_or_create()[/-][+]Bug: Nothing is returned when using either `first_or_create` or `find_by`[/+]ckhatton commentedon Mar 15, 2017
I think I may know why nothing is returned...
#L32: Shouldn't that be
return instances?#L37: Shouldn't that be
instances ? return instances.first : return nil?I can't remember in ruby if stating a variable alone (like #L32) automatically returns the value.
dominicsayers commentedon Mar 15, 2017
No, Ruby returns the last thing that was evaluated in the method. It's normal Ruby style to not use
returnunless you want to return early from a method.dominicsayers commentedon Mar 15, 2017
On the more substantive point, I copied your code fragment into a script and ran it:
and it output
as expected.
ckhatton commentedon Mar 15, 2017
Arr I think I know why... What does the angle brackets mean? As it outputs as nothing ('nil') in the logs. I know there is something there because I have tried
puts record.nil?, which returns 'false'.If I run
puts recordin the rails console, I get...If I run
puts 'Account: ' + recordin the rails console, I get...TypeError: no implicit conversion of Mailchimp::List::Member into StringAs the result is 'nil', I think that is why I don't see the output in the log.
dominicsayers commentedon Mar 15, 2017
explicitly invokes the
to_smethod ofMailchimp::List:Member(#L50-L52)'Account: ' + recordmight imply a different sort of addition and Ruby complains in case you meant something different.ckhatton commentedon Mar 15, 2017
Ar okay. So how can I get
putsto output the string to the log instead of the result 'nil'?ckhatton commentedon Mar 15, 2017
What I cannot get my head around is why
first_or_createthrowsMailchimp::Exception::Duplicatewhen run via my controller, but not when run via the rails console (when the member already exists on the list).