On Fri, May 6, 2022 at 11:48 PM Thiebo <[email protected]> wrote:
> Hi everyone,
>
> I have these classes:
>
> class User < Sequel::Model
> one_to_one :password
> one_to_many :old_password
> end
>
> class Password < Sequel::Model(:user_password_hashes)
> many_to_one :user
> end
>
> that correspond to the following migrations:
>
> create_table(:users) do
> primary_key :id, :type=>:Bignum
> String :name, null: false
> String :first_name, null: false
> citext :email, :null=>false
> constraint :valid_email, :email=>/^[^,;@ \r\n]+@[^,@; \r\n]+\.[^,@;
> \r\n]+$/
> index :email, :unique=>true
> Boolean :superuser, null: false, :default=>false
> Boolean :administrator, null: false, :default=>false
> Boolean :user, null: false, :default=>true
> DateTime :created_at, :null=>false,
> :default=>Sequel::CURRENT_TIMESTAMP
> end
>
> create_table(:user_password_hashes) do
> foreign_key :id, :users, :primary_key=>true, :type=>:Bignum,
> :unique=>true
> String :password_hash, :null=>false
> end
>
> Creating a user doesn't raise any problems and is correctly added to the
> database:
>
> user = User.create(
> name: @request_payload['nom'],
> first_name: @request_payload['prenom'],
> email: @request_payload['mail'],
> superuser: true,
> administrator: false,
> user: false
> )
>
> But adding a password raises an error. I do this:
>
> # @request_payload['pass'] is the password the user submits
> password = BCrypt::Password.create( @request_payload['pass'] )
>
> # add_password which is the model name that corresponds to the
> # table I set with this method: Sequel::Model(:user_password_hashes)
> user.add_password(password_hash: password)
>
> The error is:
>
>
>
>
>
> *NoMethodError - undefined method `add_password' for #<User
> @values={:id=>14, :name=>"foo", :first_name=>"frodo", :email=>"[email protected]
> <[email protected]>", :superuser=>true, :administrator=>false, :user=>false,
> :created_at=>2022-05-07 08:26:28.928112 +0200}>
> user.add_password(password_hash: password) ^^^^^^^^^^^^^Did you
> mean? add_old_password:*
>
> Any help is much appreciated.
>
You used "one_to_one :password". This defines password and password=
methods, not add_password and remove_password methods. You could try
"user.password = Password.new(password_hash: password)"
Honestly, I'm not sure that change is exactly what you want, since it won't
take the current password and add it to the old passwords.
Are you using Rodauth? If so, Rodauth offers better ways to do what you
want.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/CADGZSSe-%3DprFw4eaLk5%2BNYit2E9%3Da0YAvLMfgqeQ1qLAoxLvLA%40mail.gmail.com.