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

Rails 5 required belongs_to makes sash relation fail #265

Closed
tamycova opened this issue Oct 31, 2016 · 16 comments
Closed

Rails 5 required belongs_to makes sash relation fail #265

tamycova opened this issue Oct 31, 2016 · 16 comments

Comments

@tamycova
Copy link

Hi!
I just added merit to my app, when I try to seed users I get ActiveRecord::RecordInvalid: Validation failed: Sash must exist, should I manually add a Sash id when I create an user, or where does this problem come from?
Thanks!

@tute
Copy link
Member

tute commented Oct 31, 2016

A User should create its Sash if it doesn't exist.
How do you seed in users?

@tamycova
Copy link
Author

tamycova commented Oct 31, 2016

Thanks @tute for the quick reply, this is how I seed:
User.create(first_name: 'Tamara', last_name: 'Covacevich',phone: '89378899', picture: '', password: '654321', email: 'tamy_cova@hotmail.com')

Edit: Also, what about the controller on user#create? (I am using devise)

@tute
Copy link
Member

tute commented Oct 31, 2016

This is the code that initializes a sash if it doesn't exist: https://github.com/merit-gem/merit/blob/master/lib/merit/model_additions.rb#L46-L56. I don't see why an application would need to manually create the sash.

Is this code you run in the console? If not, can you paste the file that contains that line? Can you paste your User model file?

Thanks!

@tamycova
Copy link
Author

tamycova commented Oct 31, 2016

Yes! this is the code (when I run it directly in console it rollsback the transaction, if I add it in the seed then it raises a validation error, its not passing the has_merit validation in the user model).
My seed:
screen shot 2016-10-31 at 3 57 28 pm
The error:
screen shot 2016-10-31 at 3 57 21 pm
What happents when I comment out the has_merit validation (sash_id goes to nil):
screen shot 2016-10-31 at 3 45 35 pm
This is my user model:

class User < ApplicationRecord
    # Gamification
    has_merit

    # relacion de miembros user n:n group, OK
    has_many :members, dependent: :destroy
    has_many :groups, through: :members
    # relacion de admin user 1:n groups
    # podriamos poner dependant:destroy para que si se elimina el usuario grupo no quede sin admin -> futuro, OK
    # has_many :groups  
    # relacion de wheelers user n:n workouts, OK
    has_many :wheelers, dependent: :destroy
    has_many :workouts, through: :wheelers
    # relacion user 1:n places, creador, la logica de si es admin se ve en el controller, OK 
    has_many :places, dependent: :destroy
    # relacion user 1:n post, creador, dependant destroy -> future, OK
    has_many :posts, dependent: :destroy
    # relacion users 1:n comments, OK
    has_many :comments, dependent: :destroy


  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

    validates :first_name, presence: true
    validates :last_name, presence: true
    validates :phone, presence: true

    before_save :check_image

    def check_image
        begin
            url = URI.parse(picture) 
            Net::HTTP.start(url.host, url.port) do |http|
            res = http.head(url.request_uri)['Content-Type'].start_with? 'image' rescue false
            if not res
                self.picture = '/assets/default_user.jpg'
            end
        end
        rescue
            self.picture = '/assets/default_user.jpg'
        # https://www.apartmentadvisor.com/design/assets/tn3_userprofile.jpg
        end
    end

    def full_name
      "#{self.first_name} #{self.last_name}"
    end

    def reference
      "<a href='/users/#{self.id}'>#{self.first_name} #{self.last_name}</a>"
    end
end

Sorry for this long post and thank you so much for your help!!!

@tute
Copy link
Member

tute commented Oct 31, 2016

Thanks! And forgot to ask, what version of merit are you using?

@tamycova
Copy link
Author

2.4 and rails 5, if having access to my app would help let me know!

@tute
Copy link
Member

tute commented Oct 31, 2016

Aww this is biting us: http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html

Not sure how to fix right now, but that's the source of this bug in merit. Thanks for reporting!

@tute
Copy link
Member

tute commented Oct 31, 2016

If it doesn't fail on previous versions of Rails, adding , required: false in the merit side (model_additions.rb) might be a good solution.

@tamycova
Copy link
Author

Nice! I will figure it out in a while (have to leave now) and let you know how it goes!

@tute tute changed the title Sash must exist Rails 5 required belongs_to makes sash relation fail Oct 31, 2016
@tamycova
Copy link
Author

tamycova commented Nov 3, 2016

Adding optional: true to the belong_to association lets me seed and work perfectly with merit on my app (I changed the local installation). When I try to add the line to merit and run the tests with rake I get:

rake aborted!
Command failed with status (1): [ruby -w -I"lib:lib:test" -I"/Users/tamycova/.rvm/gems/ruby-2.3.1/gems/rake-11.3.0/lib" "/Users/tamycova/.rvm/gems/ruby-2.3.1/gems/rake-11.3.0/lib/rake/rake_test_loader.rb" "test/**/*_test.rb" ]
/Users/tamycova/.rvm/gems/ruby-2.3.1/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
/Users/tamycova/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/Users/tamycova/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => default => test

I think it may be a version problem?

@tute
Copy link
Member

tute commented Nov 3, 2016

Not sure. Is that the whole stacktrace you get?

You can also send a Pull Request, even if it's failing, so we inspect this in the Continuos Integration (Travis CI).

@tute
Copy link
Member

tute commented Nov 21, 2016

Closing in favor of Pull Request. Thanks!

@tute tute closed this as completed Nov 21, 2016
@nikhilgoyal22
Copy link

@tute .. I see that pull request is closed ... so whats the solution of ActiveRecord::RecordInvalid: Validation failed: Sash must exist. when creating a user on rails 5.1.4

@redtachyons
Copy link

@nikhilgoyal22 try the changes in this PR #287

@nikhilgoyal22
Copy link

@redtachyons but what should I use in my gemfile

@redtachyons
Copy link

@nikhilgoyal22
gem 'merit', github: 'redtachyons/merit'

Add this in Gemfile and check

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

4 participants