How to remove table column in Active Record - ruby

I would like to remove column from the table by using Active record. Please find the below snippet
require "active_record"
require 'sqlite3'
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test_one')
class Account < ActiveRecord::Base
table_name = "AccountBean"
primary_key = "process_id"
remove_column "xxx" // I need this type of method to remove column "xxx" from accounts table
end
Is there any class method in ActiveRecord which satisfy this requirement ?

I guess ActiveRecord presumes that changes to structure shall be done using migrations.
If you really need to, you could use the same methods rails uses in migrations to e.g. remove a column - like here
I don't recommend this :)
ActiveRecord::Base.connection.remove_column("persons", "first_name")
Within a class that would look something like:
class Account < ActiveRecord::Base
table_name = "AccountBean"
primary_key = "process_id"
connection.remove_column(table_name, "xxx")
end

Related

Cannot update record with ActiveRecord (no rails)

Hello I have the following table in my PostgreSQL database:
CREATE TABLE user_logins (token VARCHAR(512) NOT NULL, user_id UUID UNIQUE NOT NULL, create_date TIMESTAMPTZ DEFAULT clock_timestamp(), last_visit TIMESTAMPTZ DEFAULT clock_timestamp(), expire TIMESTAMPTZ DEFAULT clock_timestamp() + INTERVAL '30 DAYS');
For which I have the following ActiveRecord class
require "active_record"
class UserLogin < ActiveRecord::Base
end
I want to update the last_visit column, which I try to do like this:
def update_last_visited(user_token)
user_login = UserLogin.find_by(token: user_token)
if user_login == nil
return false
end
user_login.update(last_visit: Time.now.utc)
end
However, I get this error:
2023-02-19 11:55:21 - ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: ...logins" SET "last_visit" = $1 WHERE "user_logins"."" IS NULL
As I have understood ActiveRecord if I have the object ActiveRecord should be able to update the record, or have I miss understood something?
Should I instead do an update based on a where?
A minimal reproducible example:
require "active_record"
require "yaml"
require 'securerandom'
db_config_file = File.open("database.yml")
db_config = YAML::load(db_config_file)
ActiveRecord::Base.establish_connection(db_config)
class UserLogin < ActiveRecord::Base
end
def update_last_visited(user_token)
user_login = UserLogin.find_by(token: user_token)
if user_login == nil
return false
end
user_login.update(last_visit: Time.now.utc)
end
user_login = UserLogin.new
user_login.token = "wompas"
user_login.user_id = SecureRandom.uuid
user_login.save
update_last_visited(user_login.token)
The database.yml file:
adapter: 'postgresql',
host: 'localhost',
port: 5432
username: 'user',
password: 'password',
database: 'dbname'
Note: The project does not utilise Rails, we only use ActiveRecord for its ORM capabilities.
Your UserLogin doesn't have a primary key. But ActiveRecord expects tables to have a primary key column named id per default. See Active Record Basics / Schema Conventions in the official Rails Guides.
In your example, it feels like the user_id can be used as a primary key, because is cannot be NULL and it must be unique. You can try to configure user_id to be used by as primary key by ActiveRecord like this.
class UserLogin < ActiveRecord::Base
self.primary_key = :user_id
end
But in general, I suggest not fighting the default conventions and would just add a bigint id column to that table. While it is possible to configure custom primary key columns, it makes everything a bit more complex because you will need to remember those custom setting in other use cases – like, for example, when defining association.

How to implement Application Record in ruby without rails

I've been looking at this repository
https://github.com/stungeye/ActiveRecord-without-Rails to understand how can I implement activerecord without rails.I got some problems. At first I got this error when I tried to run this class:
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'rbuserroom')
# Can override table name and primary key
class User < ActiveRecord::Base
self.primary_key = 'user_id'
def initialize(id, email)
#user_id = id
#user_email = email
#user_room
end
def create()
self.save
end
# accessor get and set method
attr_accessor :user_room
attr_reader :user_id, :user_email
end
usr = User.new(1, "user#user")
usr.create()
but I got this error:
Traceback (most recent call last):
1: from -:25:in `<main>'
/home/felipe/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/inheritance.rb:52:in `new': wrong number of arguments (given 2, expected 0..1) (ArgumentError)
it seems that active record doesn't accept the parameters in the creation of the class, in fact after that i noticed that the classes in this example don't contain anything inside, how would active record define the columns of the tables?
i'm used to java jpa and springboot that i have to define all the attributes of the class.
besides i don't know if the active record is really working.
I just want that when I create a new user with my user class, the information persists in the database as an insert, or that it updates when I make a change to my object attribute value.
With ActiveRecord you don't need to specify the column names. It detects them from the DB.
You can just write:
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'rbuserroom')
class User < ActiveRecord::Base
self.primary_key = 'user_id'
end
usr = User.create(user_id: 1, user_email: "user#user")
You can read more about creating models in the docs. Especially in 3 Creating Active Record Models

ActiveRecord mapping to table name with schema name as prefix

Has anyone experienced this issue on mapping table in ActiveRecord when table name need a schema name as a prefix (oracle)?
Gemfile
gem 'activerecord', '4.2.4'
gem 'activerecord-oracle_enhanced-adapter', '1.6.7'
....
db_helper.rb
class Student < ActiveRecord::Base
self.abstract_class = true
self.table_name_prefix = 'OPT_ABC.'
self.table_name = 'STUDENT'
def self.list_student
puts Student.take(1) #testing
end
end
The actual table name looks like:
SELECT * FROM OPT_ABC.STUDENT;
I am able to connect to the database instance, but when the code gets line:
puts Student.take(1) # SELECT * FROM STUDENT LIMIT 1
I get the following error:
ActiveRecord::StatementInvalid:
table or view does not exist: SELECT "STUDENT".* FROM "STUDENT"
I am looking for solution on how to handle 'OPT_ABC." table prefix. Please share your solution.
It looks like the problem is that you're trying to use both self.table_name_prefix= and self.table_name= together when you should be using one OR the other.
First let's consider how both self.table_name_prefix= and self.table_name= work.
self.table_name_prefix=
According to the documentation, self.table_name_prefix= works by prepending the passed in value to the table name that ActiveRecord automatically generates based off of the name of the class.
So, if the class name is Student and you do self.table_name_prefix = 'OPT_ABC.', your table name will be OPT_ABC.STUDENTS. Note that the generated table name is plural (and ends in an s).
self.table_name=
According to the documentation, self.table_name= sets the table name explicitly. This means that it completely overrides the table name to the value that you pass in.
So, if you do self.table_name = 'OPT_ABC.STUDENT' your table name will be OPT_ABC.STUDENT.
So, given that, to set the table name to OPT_ABC.STUDENT, you should be able to simply pass the value into self.table_name like this:
class Student < ActiveRecord::Base
self.abstract_class = true
self.table_name = 'OPT_ABC.STUDENT'
def self.list_student
puts Student.take(1) #testing
end
end

ActiveRecord is showing wrong column defaults

This is my first time using ActiveRecord in a non-rails application, and I'm running into a problem. ActiveRecord is able to figure out the columns I have in my sqlite3 database, but it can't figure out the default column values for some reason.
Here is the sql description for the table in question.
-- Describe ACCOUNTS
CREATE TABLE "accounts" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT (0),
"username" TEXT NOT NULL,
"password_hash" BLOB NOT NULL,
"creation_time" INTEGER NOT NULL DEFAULT(strftime('%s', 'now')),
"expiration_time" INTEGER NOT NULL DEFAULT(strftime('%s', 'now') + 2592000)
)
I used the following code for loading my database file.
require 'active_record'
require './config.rb'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => DB_FILE
)
class Account < ActiveRecord::Base
end
When I look at the column defaults for the Account table with a REPL, this is what I get:
[10] pry(main)> Account.column_defaults
=> {"id"=>0,
"username"=>nil,
"password_hash"=>nil,
"creation_time"=>0,
"expiration_time"=>0}
I worked with ActiveRecord for a rails app before and it was smart enough to figure out the default values. For some reason, it can't figure them out now.
Am I doing something wrong here? I read that I can manually specify the default value with default :id => bla, but shouldn't ActiveRecord be able to figure out the defaults?
Update: I think I figured out a workaround. The hash returned by Account.column_defaults is writeable, and changing those elements seems to work fine.
Try doing this:
class Account < ActiveRecord::Base
after_initialize :default_values
private
def default_values
self.username ||= "default value"
#etc...
end
end

Articles/examples with test unit and active record

I'm looking to write some unit tests that will let me build/test non-stubbed CRUD functions like I can in Rails but I want to use a minimal number of gems (test unit&active record).
Anyone know of any resources that might help?
Thanks
Don't know of any resources specifically about this. To get active record working in a simple test you would just need to set up the connection details and your model classes assuming you have an existing database to work with that matches the active record conventions. Don't know what stage you are at but if you just need a simple example:
require 'rubygems'
require 'active_record'
require 'test/unit'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'db/my.db'
)
# Assuming a table like:
# CREATE TABLE people (id INTEGER PRIMARY KEY, name VARCHAR(100) NOT NULL, age INTEGER NOT NULL);
class Person < ActiveRecord::Base
end
class TestActiveRecord < Test::Unit::TestCase
def setup
#bob = Person.create(:name => 'Bob', :age => 95)
end
def teardown
#bob.destroy
end
def test_find_bob
bob = Person.find_by_name('Bob')
assert_not_nil(bob)
assert_equal(95, bob.age)
end
end
There are no other gems involved here other than those that active record itself depends on.
You'll have to work out what configuration settings you need depending on the type of database adapter you are using. If your database schema doesn't conform to the active record conventions then you will also have to specify some mappings in your model classes.

Resources