Rails - How to add a BLOB column using ActiveRecord? - ruby-on-rails-3

I need to create a BLOB column to store some text content.
I have read somewhere that I need to do the following:
class AddVersionCommentToMetaData < ActiveRecord::Migration
def self.up
add_column :meta_data, :version_comment, :binary, :limit => 10.megabyte
end
def self.down
remove_column :meta_data, :version_comment
end
end
However, it gives the following error message:
PGError: ERROR: type modifier is not allowed for type "bytea" LINE 1:
..."meta_data" ADD COLUMN "version_comment_extended" bytea(1048...
^ : ALTER
TABLE "meta_data" ADD COLUMN "version_comment_extended"
bytea(10485760)
Any idea?
Please note that I am using PostgreSQL.
Thanks!

The migration seems to be correct except the down part. It should be:
class AddVersionCommentToMetaData < ActiveRecord::Migration
def self.up
add_column :meta_data, :version_comment, :binary, :limit => 10.megabyte
end
def self.down
remove_column :meta_data, :version_comment
end
end
double check for typos. And what version of rails are you using? It works well in rails 3.0.7.

Related

How to use custom version table instead of version table?

For a project I'm currently working on I need to implement object versioning. Unfortunately I need to keep a full history of each object, so a single table solution like Papertrail would quickly become un-manageable. So I am trying to have custom version table for each object using papertrail. I followed the documentation but getting error on creating object
Model::UnknownAttributeError: unknown attribute 'event' for ArticleVersion.
Here is the implemented code :
class Article < ActiveRecord::Base
has_paper_trail class_name: 'ArticleVersion'
end
class ArticleVersion < PaperTrail::Version
self.table_name = :article_versions
end
module PaperTrail
class Version < ActiveRecord::Base
include PaperTrail::VersionConcern
self.abstract_class = true
end
end
Here is the migration for Article migrations:
class CreateArticleVersions < ActiveRecord::Migration[5.0]
def change
create_table :article_versions do |t|
t.string :title
t.text :text
t.timestamps
end
end
end
and When I am trying to create article by Article.create(text:'some text')
I am getting the above mentioned error.
Can anyone help me to figure out if I am missing anything.
create_table :article_versions do |t|
t.string :title
t.text :text
t.timestamps
end
Model::UnknownAttributeError: unknown attribute 'event' for ArticleVersion.
The ArticleVersion model lacks an event attribute because the article_versions table lacks an event column.

change_column_null for existing column

I'm attempting to change the nil possibility of a boolean attribute on the existing column :access_titles on the :profiles table. That column exists because of this migration:
class AddAccessItemsToProfiles < ActiveRecord::Migration
def self.up
add_column :profiles, :access_items, :boolean, :default => true
end
def self.down
remove_column :profiles, :access_items, :boolean, :default => nil
end
end
To change nil, I tried generating a migration as I always have:
rails g migration ChangeColumnNull :profiles :access_items :null => false
But that did nothing, so I did a standalone migration:
rails g migration AddChangeColumnNullToAccessItems
And within that I added:
class AddChangeColumnNullToAccessItems < ActiveRecord::Migration
def self.up
change_column_null :profiles, :access_items, :boolean, false
end
def self.down
change_column_null :profiles, :access_items, :boolean, true
end
end
I then ran rake db:migrate, restarted my server and saw no changes. So I tried:
class AddChangeColumnNullToAccessItems < ActiveRecord::Migration
def self.up
change_column_null :profiles, :access_items, false
end
def self.down
change_column_null :profiles, :access_items, true
end
end
Then did the same thing: rake db:migrate, restarted the server, and nothing changed.
What am I doing wrong? I was hoping to only have the boolean value of :access_items be true and false without having to dump the database.
UPDATE: Trying change_column_null :profiles, :access_items, false I got an error:
-- change_column_null(:profiles, :access_items, false)
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: column "access_items" contains null values
: ALTER TABLE "profiles" ALTER "access_items" SET NOT NULL
So, per the advice below I had to insert change_column_null :profiles, :access_items, false, true into my migration.
You can use:
change_column_null :profiles, :access_items, false, 1
The fourth parameter is optional and allows you to set the default value for the column. This is required when you have nulls in a column and you're setting the null value to false.

Specifying string fields length on Ruby on Rails

I'm not able to generate string fields with a specified length in a migration. They are always created with 255 character-length.
Anyone knows?
I think you're looking for the :limit option:
class CreateUser < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name, :limit => 10
end
end
end
Reference

rails 3.0 attr_encrypted existing database

In a rails 3.0 I need to encrypt an existing text field.
There a table memos that contains a text field "note". I've create an encrypted_note field
and added in the model:
attr_encrypted :note, :key => 'a secret key'
For now when I load an existing record the "note" is empty. I'm assuming that attr_encrypted try to decrypt...but the field has note been encrypted yet!
attr_encrypted works well for new records but wondering what would be the best strategy to encrypt the existing records?
Does instance_variable_get('#note') or read_attribute('note') work?
If so, you can probably do something like this in the Rails console:
User.all.each do |user|
user.note = user.instance_variable_get('#note')
user.save
end
Here is the trick to clear the unencrypted column as the encrypted one get populated!
in the model add:
before_update :clear_note
def clear_note
if encrypted_note != nil && read_attribute('note') != nil
write_attribute('note','')
end
end
Assuming you start with your model Thing with the un-encrypted attribute note.
1) Add a migration to add a field encrypted_note and populate it
class EncryptThing < ActiveRecord::Migration
def up
rename_column :things, :note, :old_note
add_column :things, :encrypted_note, :string
# if you want to use per-attribute iv and salt:
# add_column :things, :encrypted_note_iv, :string
# add_column :things, :encrypted_note_salt, :string
Thing.find_each do |t|
t.note = t.old_note
t.save
end
remove_column :things, :old_note
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
2) Add a line to your model to specify the encrypted attribute:
attr_encrypted :note, :key => Rails.application.config.key
# if you want to use per-attribute iv and salt, add this to the line above:
# , :mode => :per_attribute_iv_and_salt
3) run your migration
rake db:migrate

how to use auto_complete with rails3?

After searching on the web I got a problem :
I use rails3 and I do the following command
rails plugin install git://github.com/patshaughnessy/auto_complete.git
then i restart my rails server
my index.html.erb :
<%= text_field_with_auto_complete :departement, :nom, {}, {:method => :get} %>
my controller :
class AutocompController < ApplicationController
auto_complete_for :departement, :nom
end
I got a model like :
class CreateDepartements < ActiveRecord::Migration
def self.up
create_table :departements do |t|
t.column :region_id, :integer
t.column :num_dept, :string
t.column :nom, :string
end
end
def self.down
drop_table :departements
end
end
my routes.rb :
resources :autocomp, :collection => { :auto_complete_for_departement_nom => :get }
I get the following error :
No route matches {:controller=>"autocomp", :action=>"auto_complete_for_departement_nom"}
I don't get why it doesn't work? is auto_complete compatible with rails3? Or should I use jQuery?
You need to setup the route. Its missing, therefor you get this message.
Goto config/routes.rb and add your route, like:
resources : departements do
get : auto_complete_for_departement_nom, :on => :collection
end
Im using, autocomplete