I wanted to add two columns to the migration that makes the notifications for the mailboxer gem. When I place both in the migration that creates the notification and then run a migration it goes through. I then input them into the form and when I submit them there are no errors and the log shows that its take them. For some reason when I try to display them they don't show up, the only thing that is showing is the original things that were part of the migration which is subject and body. My question is how can I add columns to this migration?
Here is the migration with my two columns of lat and long added to the notifications section.
# This migration comes from mailboxer_engine (originally 20110511145103)
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :conversations do |t|
t.column :subject, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Receipts
create_table :receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :read, :boolean, :default => false
t.column :trashed, :boolean, :default => false
t.column :deleted, :boolean, :default => false
t.column :mailbox_type, :string, :limit => 25
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Notifications and Messages
create_table :notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.column :lat, :text
t.column :long, :text
t.references :sender, :polymorphic => true
t.references :object, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
end
#Indexes
#Conversations
#Receipts
add_index "receipts","notification_id"
#Messages
add_index "notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "receipts", "notifications", :name => "receipts_on_notification_id"
#Messages
add_foreign_key "notifications", "conversations", :name => "notifications_on_conversation_id"
end
def self.down
#Tables
remove_foreign_key "receipts", :name => "receipts_on_notification_id"
remove_foreign_key "notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :receipts
drop_table :conversations
drop_table :notifications
end
end
My show view looks like
%h1= conversation.subject
%ul
= content_tag_for(:li, conversation.receipts_for(current_user)) do |receipt|
- message = receipt.message
%h3= message.subject
%p= message.body
%p= message.lat
%p= message.long
= render 'messages/form', conversation: conversation
This is what comes up in the console, for lat and long you see it says null
Notification Load (0.1ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1
--- !ruby/object:Message
attributes:
id: 4
type: Message
body: game
subject: wiz
lat: !!null
long: !!null
sender_id: 1
sender_type: User
conversation_id: 2
draft: false
updated_at: 2013-03-10 04:37:54.984277000Z
created_at: 2013-03-10 04:37:54.984277000Z
notified_object_id: !!null
notified_object_type: !!null
notification_code: !!null
attachment: !!null
=> nil
I am not sure I understand exactly what you are doing, but it sounds like possibly you haven't added the two new columns to attr_accessible and they aren't being saved because of that. That's the first thing I would check (it's in the model).
Otherwise, go to the console and see if your new columns are there and see if there is data in them. That will help you find where the problem is.
Related
My schema:
create_table "location_hours", force: true do |t|
t.datetime "start_at"
t.datetime "end_at"
t.integer "location_id"
t.datetime "created_at"
t.datetime "updated_at"
end
My migration:
class ChangeLocationHourNulls < ActiveRecord::Migration
def change
change_column :location_hours, :start_at, :datetime, :null => :false
change_column :location_hours, :end_at, :datetime, :null => :false
change_column :location_hours, :location_id, :integer, :null => :false
end
end
Rake Output:
$ bundle exec rake db:migrate
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
== ChangeLocationHourNulls: migrating =======================================
-- change_column(:location_hours, :start_at, :datetime, {:null=>:false})
-> 0.0008s
-- change_column(:location_hours, :end_at, :datetime, {:null=>:false})
-> 0.0006s
-- change_column(:location_hours, :location_id, :integer, {:null=>:false})
-> 0.0032s
== ChangeLocationHourNulls: migrated (0.0067s) ==============================
-> 0.0032s
== ChangeLocationHourNulls: migrated (0.0067s) ==============================
When I check my schema file, it hasn't changed, and the database hasn't changed. Any ideas on what could cause this?
Rollback the migration ChangeLocationHourNulls.
Then change your migration as below:
class ChangeLocationHourNulls < ActiveRecord::Migration
def change
change_column :location_hours, :start_at, :datetime, :null => false
change_column :location_hours, :end_at, :datetime, :null => false
change_column :location_hours, :location_id, :integer, :null => false
end
end
Use false and not :false.
Run rake db:migrate
I believe #Kirti advise should solve the issue but i just realized there is a better option in your case, since you just want to change Nullable option:
You could use change_column_null which works like this:
class ChangeLocationHourNulls < ActiveRecord::Migration
def change
change_column_null :location_hours, :start_at, false
change_column_null :location_hours, :end_at, false
change_column_null :location_hours, :location_id, false
end
end
I am new to Ruby on Rails. I have a migration called create user
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.column :username, :string, :limit => 25, :default => "", :null => false
t.column :hashed_password, :string, :limit => 40, :default => "", :null => false
t.column :first_name, :string, :limit => 25, :default => "", :null => false
t.column :last_name, :string, :limit => 40, :default => "", :null => false
t.column :email, :string, :limit => 50, :default => "", :null => false
t.column :display_name, :string, :limit => 25, :default => "", :null => false
t.column :user_level, :integer, :limit => 3, :default => 0, :null => false
end
User.create(:username=>'test',:hashed_password=>'test',:first_name=>'test',:last_name=>'test',:email=>'test#test.com',:display_name=> 'test',:user_level=>9)
end
end
When I run rake db:migrate the table is created with the columns as mentioned above but the test data are not there
mysql>select * from users;
Empty set (0.00 sec)
EDIT I just dropped the whole database and restarted the migration and now it is showing the following error.
rake aborted!
An error has occurred, all later migrations canceled:
Can't mass-assign protected attributes: username, hashed_password, first_name, last_name, email, display_name, user_level
What am I doing wrong please help?
Thank you.
add
attr_accessible :username, :hashed_password, :first_name, :last_name, :email, :display_name, :user_level
to your user.rb
That's Rails way of prohibiting users to create or update objects via a param hash. You need to specify your User attributes as attr_accessible in your model:
example :
class User
attr_accessible :username, :firstname (etc)
end
Read more about Mass Assignment here.
just to complete the answer about testing environment. You can run rake db:test:prepare to check the migrations and load schema !
My migration right now :
class CreateActivities < ActiveRecord::Migration
def self.up
create_table :activities do |t|
t.integer :account_id, :null => false
t.integer :target_id, :null => false
t.string :target_type, :null => false
t.string :event_type, :null => false
t.integer :employee_id
t.string :name
t.timestamps
end
add_index :activities, [:target_id, :target_type]
With this I can now call a target like so :
Activity.first.target
And it will bring up the target_id, based on the target_type.
How would I do the opposite of that so that I can select a target, and if it has any associated Activities, they will show up?
Like so :
Job.find(1234).activities
# Where Job.find(1234) is the target_id of many activities.
If I understand right, this should do it:
class Job < ActiveRecord::Base
has_many :activities, :conditions => ['target_type = ?', 'Job'], :as => :target
end
Current migration:
t.string "email", :default => "", :null => false
add_index :users, :email, :unique => true
I want to create a new migration to remove the :null => false requirement and also remove the default => "" for email. Also, I would like to change the index to remove :unique => true. What's the syntax?
I haven't done much with indices, and there doesn't seem to be a change_index method on ActiveRecord::Migration, but you can try something like this:
class ChangeUserStuff < ActiveRecord::Migration
def self.up
change_column :users, :email, :default => "", :null => true
remove_index :users, :column => :email
add_index :users, :email
end
def self.down
change_column :users, :email, :default => "", :null => false
remove_index :users, :column => :email
add_index :users, :email, :unique => true
end
end
There was some funny behavior regarding changing :null options, but I believe setting them to true instead of omitting should handle it.
I'm using the composite primary keys gem from drnic and I've got a problem with it:
If I want to create a CourseOrder (with has a cpk) with the following command in my tests: course_order = CourseOrder.new(:daily_order => daily_orders(:daily_order_one_of_today_for_hans),:course => courses(:non_veg_2), :finished => false, :ordered_at => Time.now) I'll get the following error:
ActiveRecord::StatementInvalid: PGError: ERROR: null value in column "course_iddaily_order_id" violates not-null constraint
: INSERT INTO "course_orders" ("course_iddaily_order_id", "course_id", "ordered_at", "finished", "daily_order_id") VALUES (NULL, 489519433, '2011-03-01 10:19:27.169397', 'f', 594369222) RETURNING "course_iddaily_order_id"
I think that there is a course_iddaily_order_id is wrong, isn't it?
My Migration file looks like this:
class AddCourseOrder < ActiveRecord::Migration
def self.up
create_table :course_orders, :primary_key => [:course_id, :daily_order_id] do |table|
table.integer :course_id
table.integer :daily_order_id
table.datetime :ordered_at, :null => false
table.boolean :finished, :default => false, :null => false
end
end
def self.down
drop_table :course_orders
end
end
The resulting schema part looks like this:
create_table "course_orders", :primary_key => "course_iddaily_order_id", :force => true do |t|
t.integer "course_id", :null => false
t.integer "daily_order_id", :null => false
t.datetime "ordered_at", :null => false
t.boolean "finished", :default => false, :null => false
end
My Model looks like this:
class CourseOrder < ActiveRecord::Base
set_primary_keys :course_id, :daily_order_id
belongs_to :course
belongs_to :daily_order
end
I don't know whats wrong here, can you help?
I guess the create_table method doesn't takes an array for :primary_key option. I guess you can omit setting it.