Last active
August 29, 2015 14:14
-
-
Save uptownhr/358d0723cb7137728780 to your computer and use it in GitHub Desktop.
creating a new record using association pk plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
params = {"user"=>{"name"=>"zxcv", "surname"=>"", "email"=>"[email protected]", "crypted_password"=>"asdfasdf", "role"=>"admin", "status"=>"active", "account_pks"=>["1"]}} | |
@user = User.new(params[:user]) | |
### | |
error | |
PG::SyntaxError: ERROR: INSERT has more target columns than expressions | |
LINE 1: INSERT INTO "accounts_users" ("user_id", "account_id") VALUE... | |
### | |
User Model | |
class User < Sequel::Model | |
plugin :timestamps | |
plugin :validation_helpers | |
plugin :dirty | |
plugin :association_pks | |
plugin :forme | |
many_to_many :accounts | |
attr_accessor :password, :password_confirmation | |
def validate | |
validates_presence :email | |
validates_presence :role | |
validates_presence :password if password_required | |
validates_presence :password_confirmation if password_required | |
validates_length_range 4..40, :password unless password.blank? | |
errors.add(:password_confirmation, 'must confirm password') if !password.blank? && password != password_confirmation | |
validates_length_range 3..100, :email unless email.blank? | |
validates_unique :email unless email.blank? | |
validates_format /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :email unless email.blank? | |
validates_format /[A-Za-z]/, :role unless role.blank? | |
end | |
# Callbacks | |
def before_save | |
encrypt_password | |
end | |
## | |
# This method is for authentication purpose. | |
# | |
def self.authenticate(email, password) | |
account = filter(Sequel.function(:lower, :email) => Sequel.function(:lower, email)).first | |
account && account.has_password?(password) ? account : nil | |
end | |
## | |
# Replace ActiveRecord method. | |
# | |
def self.find_by_id(id) | |
self[id] rescue nil | |
end | |
def has_password?(password) | |
::BCrypt::Password.new(self.crypted_password) == password | |
end | |
private | |
def encrypt_password | |
if self.column_changed?(:crypted_password) || !self.id | |
self.crypted_password = ::BCrypt::Password.create(self.crypted_password) | |
end | |
end | |
def password_required | |
self.crypted_password.blank? || password.present? | |
end | |
end | |
Sequel.migration do | |
up do | |
create_table :users do | |
primary_key :id | |
String :first_name | |
String :last_name | |
String :email | |
String :crypted_password | |
String :role | |
String :user_status | |
String :note | |
end | |
end | |
down do | |
drop_table :users | |
end | |
end | |
Sequel.migration do | |
up do | |
create_table :accounts do | |
primary_key :id | |
Fixnum :user_id | |
String :account_status | |
String :account_name | |
DateTime :created_at | |
DateTime :updated_at | |
end | |
end | |
down do | |
drop_table :accounts | |
end | |
end | |
Sequel.migration do | |
up do | |
create_table :accounts_users do | |
Fixnum :user_id | |
Fixnum :account_id | |
end | |
end | |
down do | |
drop_table :accounts_users | |
end | |
end | |
Author
uptownhr
commented
Jan 27, 2015
ERROR - PG::SyntaxError: ERROR: INSERT has more target columns than expressions
LINE 1: INSERT INTO "accounts_users" ("user_id", "account_id") VALUE...
^: INSERT INTO "accounts_users" ("user_id", "account_id") VALUES (1)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment