Ruby on Rails generates model field:type – what are the options for field:type?
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references See the table definitions section.
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references See the table definitions section.
Rails 4.x When you already have users and uploads tables and wish to add a new relationship between them. All you need to do is: just generate a migration using the following command: rails g migration AddUserToUploads user:references Which will create a migration file as: class AddUserToUploads < ActiveRecord::Migration def change add_reference :uploads, :user, index: … Read more
It appears that the :include functionality was changed with Rails 2.1. Rails used to do the join in all cases, but for performance reasons it was changed to use multiple queries in some circumstances. This blog post by Fabio Akita has some good information on the change (see the section entitled “Optimized Eager Loading”).
It’s destroy and destroy_all methods, like user.destroy User.find(15).destroy User.destroy(15) User.where(age: 20).destroy_all User.destroy_all(age: 20) Alternatively you can use delete and delete_all which won’t enforce :before_destroy and :after_destroy callbacks or any dependent association options. User.delete_all(condition: ‘value’) will allow you to delete records without a primary key Note: from @hammady’s comment, user.destroy won’t work if User model has … Read more
Rails 4+ ActiveRecord 4.0 and above adds where.not so you can do this: Foo.includes(:bar).where.not(‘bars.id’ => nil) Foo.includes(:bar).where.not(bars: { id: nil }) When working with scopes between tables, I prefer to leverage merge so that I can use existing scopes more easily. Foo.includes(:bar).merge(Bar.where.not(id: nil)) Also, since includes does not always choose a join strategy, you should … Read more
I believe what you are looking for is assign_attributes. It’s basically the same as update_attributes but it doesn’t save the record: class User < ActiveRecord::Base attr_accessible :name attr_accessible :name, :is_admin, :as => :admin end user = User.new user.assign_attributes({ :name => ‘Josh’, :is_admin => true }) # Raises an ActiveModel::MassAssignmentSecurity::Error user.assign_attributes({ :name => ‘Bob’}) user.name # … Read more
You want to call .name on the object’s class: result.class.name
Here’s an example: class RenameOldTableToNewTable < ActiveRecord::Migration def self.up rename_table :old_table_name, :new_table_name end def self.down rename_table :new_table_name, :old_table_name end end I had to go and rename the model declaration file manually. Edit: In Rails 3.1 & 4, ActiveRecord::Migration::CommandRecorder knows how to reverse rename_table migrations, so you can do this: class RenameOldTableToNewTable < ActiveRecord::Migration def change … Read more
Here are all the Rails 4 (ActiveRecord migration) datatypes: :binary :boolean :date :datetime :decimal :float :integer :bigint :primary_key :references :string :text :time :timestamp Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column These are the same as with Rails 3. If you use PostgreSQL, you can also take advantage of these: :hstore :json :jsonb :array :cidr_address :ip_address :mac_address They are stored as … Read more
There are several issues with each of the available methods, but I believe that defining an after_initialize callback is the way to go for the following reasons: default_scope will initialize values for new models, but then that will become the scope on which you find the model. If you just want to initialize some numbers … Read more