Re: _add method in associated tables

2022-11-07 Thread Thiebo
Hi, 

Sorry for the late reply, but unless I misunderstand the documentation, I'm 
under the impression is user the method 'create' for one_to_one relations, 
not 'new' as you pointed out in your message : 

https://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html#label-Methods+Added

If I'm wrong, excuse me. 

Hope this helps. 



Le samedi 7 mai 2022 à 09:59:14 UTC+2, Jeremy Evans a écrit :

> On Fri, May 6, 2022 at 11:48 PM Thiebo  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 #> @values={:id=>14, :name=>"foo", :first_name=>"frodo", 
>> :email=>"tf...@moi.fr", :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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/af390880-67e5-450c-942f-cf14ca7b4124n%40googlegroups.com.


Re: _add method in associated tables

2022-05-15 Thread Thiebo
Hi Jeremy, 
a bit late: many thanks for your answer. 
I moved to Rodauth since. 

Le samedi 7 mai 2022 à 09:59:14 UTC+2, Jeremy Evans a écrit :

> On Fri, May 6, 2022 at 11:48 PM Thiebo  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 #> @values={:id=>14, :name=>"foo", :first_name=>"frodo", 
>> :email=>"tf...@moi.fr", :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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/86216cda-1a39-45c3-9e4b-a0676e5a1184n%40googlegroups.com.


Re: _add method in associated tables

2022-05-07 Thread Jeremy Evans
On Fri, May 6, 2022 at 11:48 PM Thiebo  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 # @values={:id=>14, :name=>"foo", :first_name=>"frodo", :email=>"tf...@moi.fr
> ", :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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSe-%3DprFw4eaLk5%2BNYit2E9%3Da0YAvLMfgqeQ1qLAoxLvLA%40mail.gmail.com.