rails model has_many of itself

This is a self-referential model, you can try something like this: class Event < ActiveRecord::Base belongs_to :parent, :class_name => “Event”, :foreign_key => “parent_event_id” has_many :child_events, :class_name => “Event”, :foreign_key => “child_event_id” end That way, you can call @event.parent to get an ActiveRecord Event object and @event.child_events to get an ActiveRecord collection of Event objects

How to implement Active Record inheritance in Ruby on Rails?

Rails supports Single Table Inheritance. From the AR docs: Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this: class Company < ActiveRecord::Base; end class Firm < Company; end class Client … Read more

Possible to have “polymorphic has_one” relationship in rails?

I don’t believe you’re in need of a has_one association here, the belongs_to should be what you’re looking for. In this case, you’d want a target_id and target_type column on your Campaign table, you can create these in a rake with a t.references :target call (where t is the table variable). class Campaign < ActiveRecord::Base … Read more

What is the difference between build and new on Rails?

new is for a new instance of a specific model: foo = Foo.new build is for creating a new instance within an AR association: bar = foo.build_bar # (has_one or belongs_to) or bar = foo.bars.build # (has\_many, habtm or has_many :through) http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Update Per @toklands’s suggestion, build and new are aliases as defined in ActiveRecord::Relation: … Read more

How to use long id in Rails applications?

Credits to http://moeffju.net/blog/using-bigint-columns-in-rails-migrations class CreateDemo < ActiveRecord::Migration def self.up create_table :demo, :id => false do |t| t.integer :id, :limit => 8 end end end See the option :id => false which disables the automatic creation of the id field The t.integer :id, :limit => 8 line will produce a 64 bit integer field

How can I test ActiveRecord::RecordNotFound in my rails app?

There are two things you can do. The first is to let ActionController provide the default action when it rescues ActiveRecord::RecordNotFound: class PostsControllerTest < ActionController::TestCase test “raises RecordNotFound when not found” do assert_raises(ActiveRecord::RecordNotFound) do get :show, :id => 1234 end end end Using this method, you can’t assert what gets rendered. You have to trust … Read more

How to tell if already within a database transaction in ruby on rails?

You can use ActiveRecord::Base.connection.open_transactions to see if your method is executed in a transaction. ActiveRecord::Base.connection.open_transactions == 0 implies that your method is not executed in a transaction. Anything greater than 0 would imply that your method is executed in a transaction. For example ActiveRecord::Base.connection.open_transactions > 0 Update: from rails documentation all database statements in the … Read more

How to test model’s callback method independently?

Callback and Callback behavior are independent tests. If you want to check an after_save callback, you need to think of it as two things: Is the callback being fired for the right events? Is the called function doing the right thing? Assume you have the Article class with many callbacks, this is how you would … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)