I'm trying to add a user reference to my post tables with following code:
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_reference :posts, :user, index: true
end
end
but I've received an error message:
undefined method 'add_reference'
Anyone knows how to solve this?
I'm using Rails 3.2.13
In Rails 3 you must do it like so
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
add_index :posts, :user_id
end
end
Only in Rails 4 you can do it the way you posted.
add_reference is specific to rails 4.0.0, so you should try this instead :
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
add_index :posts, :user_id
end
end
this is a great post about this subject
Your migration should be
rails generate migration AddUserRefToPosts user:references
How about this:
def change
change_table :posts do |p|
p.references :user, index: true
end
end
This method apperead in Rails 4.0
I think you may create some monkey patch with this functionality for Rails 3.2
Related
I insert a column in my sqlite with a wrong type "stringimage".
How can I change the column type to string?
I tried change_column :users, :uid, :string
and
def up
change_table :users do |t|
t.change :uid, :stringimage
end
end
def down
change_table :users do |t|
t.change :uid, :string
end
end
but it doesn't works.
I tried many things but none of it works, maybe because I'm using rails 5.
You Need to write following two definitions into your migration :
def up
change_column :my_table, :my_column, :string
end
def down
change_column :my_table, :my_column, :stringimage
end
Note that change_column is an irreversible migration. It will cause an error if you try to rollback. To prevent this, modify the usual change method in the migration to use two separate up and down methods like this:
class ChangeUsersUidType < ActiveRecord::Migration
def up
change_column :users, :uid, :string
end
def down
change_column :users, :uid, :stringimage
end
end
If you liked this answer you can read more in this article: https://kolosek.com/rails-change-database-column.
You can try this:
change_column(table_name, column_name, type, options): Changes the column to a different type using the same parameters as add_column.
I'm fetching a record by the code just like these
#community = Community.find_by_community_name(params[:community_name])
#user = User.find_by_username(params[:username])
I want to make it faster loading, so I'm thinking of adding index to them just like this.
If I do rake db:migrate, does it reindex to the existing records also?
Or just the records that will be created from now on?
Do it improve the speed of loading by adding index just like this?
class AddIndexToCommunity < ActiveRecord::Migration
def self.up
add_index :communities, [:community_name, :title, :body], :name=>:communities_idx
end
def self.down
remove_index :communities, [:community_name, :title, :body], :name=>:communities_idx
end
end
class AddIndexToUser < ActiveRecord::Migration
def self.up
add_index :users, [:username, :nickname, :body], :name=>:users_idx
end
def self.down
remove_index :users, [:username, :nickname, :body], :name=>:users_idx
end
end
rake db:migrate will perform database migrations and apply indeces immediately.You should apply indeces only to columns which you use in searching. Remember that indeces add time penalty on insert and update operations. If you load records on by their names, add indeces only to names:
class AddIndexToCommunity < ActiveRecord::Migration
def self.up
add_index :communities, :community_name
end
def self.down
remove_index :communities, :community_name
end
end
class AddIndexToUser < ActiveRecord::Migration
def self.up
add_index :users, :username
end
def self.down
remove_index :users, :username
end
end
I have a table and I had to add a migration script to add rows in the table.
Please help with the rails generate migration command to insert data into the table.
Thanks,
Ramya.
You can write regular ruby code inside a migration. So you can simply do something like this:
class Foo < ActiveRecord::Migration
def self.up
User.create(:username => "Hello", :role => "Admin")
end
def self.down
User.delete_all(:username => "Hello")
end
end
Just write regular ruby inside your migration same as you would in pry or rails console.
The code helped me is the sql statement as show
In migration file
def up
execute("insert into salary_ranges(salary_range) values('Above 2000');")
end
class AddFieldInUsers < ActiveRecord::Migration
def self.up
add_column :users, :admin, :boolean, :null => false, :default => 0
end
def self.down
remove_column :users
end
end
I am making my first steps with the Rails plugin 'paperclip' an therefor watched the RailsCast #134: http://railscasts.com/episodes/134-paperclip
Did everthing the same, except that I'm running rails 3.0.9 and installed paperclip (2.3.15) via adding it to the Gemfile.
Until 3:00 of the cast, everything works fine. But after reloading the show-page, I get the "missing" image instead of the uploaded image. Also, inside of the 'public' directory nothing new has been created.
Any hints?
Update: As requested here the relevant code:
Gemfile:
…
gem 'paperclip'
gem 'rails', '3.0.9'
…
config/routes.rb:
Foobar::Application.routes.draw do
resources :books
end
app/models/book.rb:
class Book < ActiveRecord::Base
has_attached_file :cover
attr_accessor :cover_file_name
end
app/controllers/books_controller.rb:
# nothing changed here after scaffolding
app/views/books/_form.html.erb:
<%= form_for(#book, :html => { :multipart => true}) do |f| %>
…
<div class="field">
<%= f.file_field :cover %>
</div>
app/views/books/show.html.erb:
…
<%= image_tag «book.cover.url %>
…
db/migrate/..._create_books.rb:
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :books
end
end
db/migrate/..._ad_attachment_cover_to_book.rb:
class AddAttachmentCoverToBook < ActiveRecord::Migration
def self.up
add_column :books, :cover_file_name, :string
add_column :books, :cover_content_type, :string
add_column :books, :cover_file_size, :integer
add_column :books, :cover_updated_at, :datetime
end
def self.down
remove_column :books, :cover_file_name
remove_column :books, :cover_content_type
remove_column :books, :cover_file_size
remove_column :books, :cover_updated_at
end
end
I started up with "rails generate paperclip book cover" after having scaffold "book"
I think that the attr_accessor :cover_file_name creates a conflict for the table column with the same name. Try removing that line. Can't find anything else that should cause any problems.
People I think I already gave the solution when you appear this error is because the cover column does not have permission to enter this is solved in the controller of articles in the params_article can only manipulate the title and the body to this also adds to it To the column cover to me I stay like this
Def articulo_params params.require (: article) .permit (: title,: body,: cover)
End
I've been searching for a while now, but google isn't really helping me.
The ArgumentError Unknown key(s): client_id appears in the ProjectsController:
# projects_controller.rb
class Management::ProjectsController < Management::ManagementController
def index
#projects = Project.find( :client_id => current_user.client )
end
end
This is the project model:
# project.rb
class Project < ActiveRecord::Base
belongs_to :client
end
This is the client model:
# client.rb
class Client < ActiveRecord::Base
has_many :projects
end
And finally, the migration:
# 20110404155917_create_projects.rb
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.string :name
t.datetime :date
t.text :description
t.integer :client_id
t.timestamps
end
end
def self.down
drop_table :projects
end
end
Should be possible, right?
Can't see what I'm missing here..
Anyone got a suggestion?
Thanks!
Use
#projects = Project.where( :client_id => current_user.client.id)
or
#projects = Project.find_by_client_id(current_user.client.id)
or you could do
#projects = current_user.client.projects
Little bit cleaner perhaps?