Is there a way to get a collection of all the Models in your Rails app?

The whole answer for Rails 3, 4 and 5 is: If cache_classes is off (by default it’s off in development, but on in production): Rails.application.eager_load! Then: ActiveRecord::Base.descendants This makes sure all models in your application, regardless of where they are, are loaded, and any gems you are using which provide models are also loaded. This … Read more

Determine what attributes were changed in Rails after_save callback?

Rails 5.1+ Use saved_change_to_published?: class SomeModel < ActiveRecord::Base after_update :send_notification_after_change def send_notification_after_change Notification.send(…) if (saved_change_to_published? && self.published == true) end end Or if you prefer, saved_change_to_attribute?(:published). Rails 3–5.1 Warning This approach works through Rails 5.1 (but is deprecated in 5.1 and has breaking changes in 5.2). You can read about the change in this pull … Read more

AngularJS – Binding radio buttons to models with boolean values

The correct approach in Angularjs is to use ng-value for non-string values of models. Modify your code like this: <label data-ng-repeat=”choice in question.choices”> <input type=”radio” name=”response” data-ng-model=”choice.isUserAnswer” data-ng-value=”true” /> {{choice.text}} </label> Ref: Straight from the horse’s mouth

Rails find_or_create_by more than one attribute?

Multiple attributes can be connected with an and: GroupMember.find_or_create_by_member_id_and_group_id(4, 7) (use find_or_initialize_by if you don’t want to save the record right away) Edit: The above method is deprecated in Rails 4. The new way to do it will be: GroupMember.where(:member_id => 4, :group_id => 7).first_or_create and GroupMember.where(:member_id => 4, :group_id => 7).first_or_initialize Edit 2: Not … Read more

Doctrine2: Best way to handle many-to-many with extra columns in reference table

I’ve opened a similar question in the Doctrine user mailing list and got a really simple answer; consider the many to many relation as an entity itself, and then you realize you have 3 objects, linked between them with a one-to-many and many-to-one relation. http://groups.google.com/group/doctrine-user/browse_thread/thread/d1d87c96052e76f7/436b896e83c10868#436b896e83c10868 Once a relation has data, it’s no more a relation … Read more

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