Skip to content

Instantly share code, notes, and snippets.

@uptownhr
Last active August 29, 2015 14:14
Show Gist options
  • Save uptownhr/358d0723cb7137728780 to your computer and use it in GitHub Desktop.
Save uptownhr/358d0723cb7137728780 to your computer and use it in GitHub Desktop.
creating a new record using association pk plugin
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
@uptownhr
Copy link
Author

sequel (4.17.0) lib/sequel/core.rb:111:in `convert_exception_class'
  sequel (4.17.0) lib/sequel/database/misc.rb:423:in `raise_error'
  sequel (4.17.0) lib/sequel/adapters/postgres.rb:522:in `rescue in check_database_errors'
  sequel (4.17.0) lib/sequel/adapters/postgres.rb:519:in `check_database_errors'
  sequel (4.17.0) lib/sequel/adapters/postgres.rb:323:in `block in execute'
  sequel (4.17.0) lib/sequel/database/connecting.rb:250:in `block in synchronize'
  sequel (4.17.0) lib/sequel/connection_pool/threaded.rb:85:in `hold'
  sequel (4.17.0) lib/sequel/database/connecting.rb:250:in `synchronize'
  sequel (4.17.0) lib/sequel/adapters/postgres.rb:323:in `execute'
  sequel (4.17.0) lib/sequel/database/query.rb:50:in `execute_dui'
  sequel (4.17.0) lib/sequel/dataset/actions.rb:917:in `execute_dui'
  sequel (4.17.0) lib/sequel/dataset/actions.rb:837:in `block (2 levels) in _import'
  sequel (4.17.0) lib/sequel/dataset/actions.rb:837:in `block in _import'
  sequel (4.17.0) lib/sequel/database/transactions.rb:97:in `block in transaction'
  sequel (4.17.0) lib/sequel/database/connecting.rb:250:in `block in synchronize'
  sequel (4.17.0) lib/sequel/connection_pool/threaded.rb:85:in `hold'
  sequel (4.17.0) lib/sequel/database/connecting.rb:250:in `synchronize'
  sequel (4.17.0) lib/sequel/database/transactions.rb:89:in `transaction'
  sequel (4.17.0) lib/sequel/dataset/actions.rb:837:in `_import'
  sequel (4.17.0) lib/sequel/adapters/shared/postgres.rb:1460:in `_import'
  sequel (4.17.0) lib/sequel/dataset/actions.rb:291:in `import'
  sequel (4.17.0) lib/sequel/plugins/association_pks.rb:89:in `block (2 levels) in def_many_to_many'
  sequel (4.17.0) lib/sequel/model/base.rb:2011:in `block in checked_transaction'
  sequel (4.17.0) lib/sequel/database/transactions.rb:126:in `_transaction'
  sequel (4.17.0) lib/sequel/database/transactions.rb:100:in `block in transaction'
  sequel (4.17.0) lib/sequel/database/connecting.rb:250:in `block in synchronize'
  sequel (4.17.0) lib/sequel/connection_pool/threaded.rb:98:in `hold'
  sequel (4.17.0) lib/sequel/database/connecting.rb:250:in `synchronize'
  sequel (4.17.0) lib/sequel/database/transactions.rb:89:in `transaction'
  sequel (4.17.0) lib/sequel/model/base.rb:2011:in `checked_transaction'
  sequel (4.17.0) lib/sequel/plugins/association_pks.rb:75:in `block in def_many_to_many'
  sequel (4.17.0) lib/sequel/model/base.rb:2098:in `block in set_restricted'
  sequel (4.17.0) lib/sequel/model/base.rb:2095:in `set_restricted'
  sequel (4.17.0) lib/sequel/model/base.rb:1552:in `set'
  sequel (4.17.0) lib/sequel/model/base.rb:2060:in `initialize_set'
  sequel (4.17.0) lib/sequel/plugins/dirty.rb:212:in `initialize_set'
  sequel (4.17.0) lib/sequel/model/base.rb:1163:in `initialize'
  admin/controllers/users.rb:19:in `block (2 levels) in <top (required)>'
  padrino-core (0.12.4) lib/padrino-core/application/routing.rb:504:in `block in route'
  padrino-core (0.12.4) lib/padrino-core/ext/http_router.rb:33:in `block (3 levels) in process_destination_path'
  sinatra (1.4.5) lib/sinatra/base.rb:985:in `route_eval'
  padrino-core (0.12.4) lib/padrino-core/ext/http_router.rb:33:in `block (2 levels) in process_destination_path'
  padrino-core (0.12.4) lib/padrino-core/ext/http_router.rb:33:in `block in process_destination_path'
  padrino-core (0.12.4) lib/padrino-core/ext/http_router.rb:8:in `process_destination_path'
  (eval):1778:in `block in call'
  (eval):1767:in `call'
  http_router (0.11.1) lib/http_router.rb:288:in `raw_call'
  padrino-core (0.12.4) lib/padrino-core/application/routing.rb:931:in `route!'
  padrino-core (0.12.4) lib/padrino-core/application/routing.rb:915:in `block in dispatch!'
  sinatra (1.4.5) lib/sinatra/base.rb:1058:in `block in invoke'
  sinatra (1.4.5) lib/sinatra/base.rb:1058:in `invoke'
  padrino-core (0.12.4) lib/padrino-core/application/routing.rb:913:in `dispatch!'
  sinatra (1.4.5) lib/sinatra/base.rb:898:in `block in call!'
  sinatra (1.4.5) lib/sinatra/base.rb:1058:in `block in invoke'
  sinatra (1.4.5) lib/sinatra/base.rb:1058:in `invoke'
  sinatra (1.4.5) lib/sinatra/base.rb:898:in `call!'
  sinatra (1.4.5) lib/sinatra/base.rb:886:in `call'
  better_errors (2.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.0) lib/better_errors/middleware.rb:57:in `call'
  rack-protection (1.5.3) lib/rack/protection/xss_header.rb:18:in `call'
  rack-protection (1.5.3) lib/rack/protection/base.rb:49:in `call'
  rack-protection (1.5.3) lib/rack/protection/base.rb:49:in `call'
  rack-protection (1.5.3) lib/rack/protection/json_csrf.rb:18:in `call'
  rack-protection (1.5.3) lib/rack/protection/base.rb:49:in `call'
  rack-protection (1.5.3) lib/rack/protection/base.rb:49:in `call'
  rack-protection (1.5.3) lib/rack/protection/frame_options.rb:31:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  padrino-core (0.12.4) lib/padrino-core/reloader/rack.rb:22:in `call'
  padrino-core (0.12.4) lib/padrino-core/logger.rb:420:in `call'
  sinatra (1.4.5) lib/sinatra/show_exceptions.rb:21:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  sinatra (1.4.5) lib/sinatra/base.rb:2014:in `call'
  sinatra (1.4.5) lib/sinatra/base.rb:1478:in `block in call'
  sinatra (1.4.5) lib/sinatra/base.rb:1788:in `synchronize'
  sinatra (1.4.5) lib/sinatra/base.rb:1478:in `call'
  padrino-core (0.12.4) lib/padrino-core/router.rb:84:in `block in call'
  padrino-core (0.12.4) lib/padrino-core/router.rb:75:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/uptown/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /Users/uptown/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /Users/uptown/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

@uptownhr
Copy link
Author

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