Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation failed: Visit must exist #276

Closed
jfrankov opened this issue May 5, 2017 · 10 comments
Closed

Validation failed: Visit must exist #276

jfrankov opened this issue May 5, 2017 · 10 comments

Comments

@jfrankov
Copy link

jfrankov commented May 5, 2017

I just installed ahoy on a rails 5 project, added this line to the create action of my controller:

ahoy.track "Created an action", title: "Call"

and I get "Validation failed: Visit must exist". My visit.rb model already has this line in it:

belongs_to :user, optional: true

...so that might not matter.

Any ideas?

@ankane
Copy link
Owner

ankane commented May 7, 2017

Hey @jfrankov, you can add optional: true to belongs_to :visit to fix the error. However, it's not ideal having events that don't have a visit. One option is to have Ahoy create a visit if there isn't already one.

@jfrankov
Copy link
Author

Thanks for the reply. I'll play with it some more.

@skunkworker
Copy link

I just had this issue and modifying event.rb to have:

    belongs_to :visit, optional: true
    belongs_to :user, optional: true

fixed it for me. Ruby 2.4.1, Rails 5.1.1 & ahoy_matey 1.6.0

buren added a commit to justarrived/just_match_api that referenced this issue Aug 16, 2017
@ghost
Copy link

ghost commented Sep 18, 2017

@ankane I think it's caused because there's no visit at the time when after_action :track_action (from the README example) is called for the first time.

@edusantana
Copy link

Same thing happened with me.

@joshuajansen
Copy link

Same here, couldn't figure out what was causing this.. Eventually I went for calling the following method before_action:

def ensure_visit
  return if ahoy.visit
  ahoy.track_visit
end

@wangthony
Copy link

Are you using visitable? You should be able to call visitable optional: true in your model.

However, there is a bug in Ahoy::Model#visitable that causes this for me:

def visitable(name = nil, options = {})
  if name.is_a?(Hash)
    name = nil
    options = name     # <---- will always be nil - this line should be switched with the previous line
  end

The workaround is to call visitable :visit, optional: true or visitable nil, optional: true

@mikgry
Copy link

mikgry commented Feb 22, 2018

I have the same issue. IMO visitable :visit, optional: true is not good solution. Why is there missing visit?

@mikgry
Copy link

mikgry commented Feb 22, 2018

#240

@ankane
Copy link
Owner

ankane commented Feb 26, 2018

Ahoy 2.0 (just released) attempts to create a visit if one doesn't exist before creating an event, which should fix this. You can do this in Ahoy 1 with:

class Ahoy::Store
  def track_event(name, properties, options)
    ahoy.track_visit unless visit
    super
  end
end

@ankane ankane closed this as completed Feb 26, 2018
@ankane ankane mentioned this issue Feb 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants