Specifying string fields length on Ruby on Rails - ruby-on-rails-3

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

Related

Rails associations has_many through ban/archive solution?

I'm new in Rails and am working on a problem. I have two tables:
Shoes and Socks
A Shoe can have many Socks, but only one active Sock. Other Socks are inactive. All Socks are also unique with unique patterns. I want to make sure I don't have socks with the same pattern. I think I can do this three ways
1) Using an additional column in table socks to represent the active sock
class Shoe < ActiveRecord::Base
has_many :socks
end
class Sock < ActiveRecord::Base
belongs_to :shoe
end
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :size
t.timestamps null: false
end
create_table :socks do |t|
t.belongs_to :shoe, index:true
t.string :pattern
t.boolean :active
t.timestamps null: false
end
end
end
This seems fairly simple, but cumbersome. I would search socks with shoe_id, and pull out the active_sock and return it's pattern. I think I would index [active_sock, shoe_id] in an array to speed it up.
2) Using an additional table to archive inactive socks
class Shoe < ActiveRecord::Base
has_many :socks
has_many :inactive_socks
end
class Sock < ActiveRecord::Base
belongs_to :Shoe
end
class Inactive_sock < ActiveRecord::Base
belongs_to :Shoe
end
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.timestamps null: false
end
create_table :socks do |t|
t.belongs_to :shoe, index:true
t.string :pattern
t.timestamps null: false
end
create_table :inactive_socks do |t|
t.belongs_to :shoe, index:true
t.string :pattern
t.timestamps null: false
end
end
end
This seems cumbersome as well, but when you are just dealing with active socks easy to use and fast. But when buying a new sock, I have to check the pattern with both tables.
3) Using a has_many :through relationship
class Shoe < ActiveRecord::Base
has_many :active_socks
has_many :socks, through: active_socks
end
class Active_Sock < ActiveRecord::Base
belongs_to :Shoe
belongs_to :Sock
end
class Sock < ActiveRecord::Base
has_many :active_socks
has_many :shoes, through: active_socks
end
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.timestamps null: false
end
create_table :socks do |t|
t.string :pattern
t.timestamps null: false
end
create_table :active_socks do |t|
t.belongs_to :shoe, index: true
t.belongs_to :sock, index: true
t.string :pattern
t.boolean :active
t.timestamps null: false
end
end
end
This seems like option 2, but I feel like I'm using Rails tools to make it less cumbersome. When I'm searching for patterns I'm just checking the socks table, when I'm searching for the one active_sock I'm just searching active_socks table.
I've read up on similar posts, and it seems options 1 and 2 are commonly used in closed_accounts, banning users, banning posts, archiving etc. Situations where you need to differentiate data that is only slightly different. The choice there seems to be look at what you need and choose the option 1 or 2 that best fits you.
My understanding for has_many through situations seems to be when you have a relationship and you need extra meta data you can use it. I think that fits this situation.
Did I set up option 1 correctly and am I right that indexing the array of [shoe_id and active] will give me a faster search? Is option 3 an appropriate use of has_many through? Would my explanation of option 3 work?
What are your usage patterns? I'm guessing you just want to be able to find the active Sock given a Shoe, and if a given Sock is active or inactive. To quickly find the active Sock of a given Shoe, you merely need to give the Sock a foreign key to its Shoe with the belongs_to association.
class Sock < ActiveRecord::Base
belongs_to :shoe
end
And to find out if a Sock is active or inactive, give its owner shoe a reference to its active sock like so:
class Shoe < ActiveRecord::Base
belongs_to :sock
end
Now, you can just go to its owner Shoe and check if the Shoe's active sock is the current Sock or not. E.g.
def is_active
owner_shoe.active_sock == self
Basically, associate them with foreign keys and you're good to go!
P.S. you pluralized Socks but the Rails convention is to use singular for model names.
EDIT: You asked for a migration so here's one for the code above. Caveat: I haven't done migrations in a long time in Rails so something might be off.
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.belongs_to :active_sock, foreign_key: "sock_id"
t.string :size
t.timestamps null: false
end
create_table :socks do |t|
t.belongs :owner_shoe, foreign_key: "shoe_id"
t.string :pattern
end
end
end

using activeuuid gem for existing database in rails

I need to implement activeuuid gem to have UUIDs instead of default Rails ids. we can implement it for creating new migration as:
class CreateStudents < ActiveRecord::Migration
def change
create_table :students, :id => false do |t|
t.uuid :id, :primary_key => true
t.string :name
t.string :email
t.timestamps
end
end
end
And in model we include ActiveUUID::UUID as:
class Student < ActiveRecord::Base
attr_accessible :email, :name
include ActiveUUID::UUID
end
Now I already have a database so how can I implement the activeuuid gem to have UUIDs instead of default Rails ids for existing DB?
Need to make changes in all migrations or what?
Need help in this regard. thanks
The UUID is stored as a binary field w/ 16 positions as I found here: https://github.com/jashmenn/activeuuid/blob/master/lib/activeuuid/patches.rb#L62
It worked for me (existing table without records):
def change
reversible do |dir|
change_table :payments do |t|
dir.up { t.change :id, :binary, limit: 16, :primary_key => true }
dir.down { t.change :id, :integer }
end
end
end
Don't forget to add those lines to your model as well:
include ActiveUUID::UUID
natural_key :at_least_one_field_here
More info in the github repo: https://github.com/jashmenn/activeuuid/

Unique model field at databse level in Rails 3.1+ migration?

Is there a way to specify a database level uniqueness constraint on a field in a Rails 3.1+ migration? I know ActiveRecord's validates_uniqueness_of would work, but I'm curious to know if this can be specified elsewhere.
For example, in order to specify that the 'login' field should be unique in a migration such as ...
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :login
t.timestamps
end
end
end
... I'd like to add an option like :unique => true.
There's nothing mentioned in the Documentation so I assume that's not a great place/way to do things, but does anyone know how this might be achieved (even if it's manually added to the resulting schema)? Or is validates_uniqueness_of the preferred way to go?
You can add unique index so:
def change
create_table :users do |t|
t.string :name
t.string :login
t.timestamps
end
add_index :users, :login, :unique => true
end

How can I use UTF8 characters in Rails migration?

creating a simple model with a migration like the following will break the rake db:migrate tasks:
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :title
t.float :price, :default => "0.00"
t.string :currency, :default => "€"
t.timestamps
end
end
end
Exchanging the Euro sign with EUR fixes the issue temporary but in general I'd love to understand how I could work with € as default value there.
Cheers
Put this on the first line of your migration file:
# encoding: utf-8

how to generate a unique debitnote number automatically

in my rails3 app i have a debitnote controller ,model and views created by scaffold.
i show you the migration:
class CreateDebitnotes < ActiveRecord::Migration
def self.up
create_table :debitnotes do |t|
t.string :debit_note_number
t.date :date
t.decimal :amount, :precision =>18, :scale => 2
t.string :debit_account_of
t.string :debit_for
t.text :description
t.timestamps
end
end
def self.down
drop_table :debitnotes
end
end
in my app i want to automatically generate a unique debitnote number when user open new form to create a deb
You may use UUID generator github repo