Ruby on Rails: how to get error messages from a child resource displayed?

Add a validation block in the School model to merge the errors: class School < ActiveRecord::Base has_many :students validate do |school| school.students.each do |student| next if student.valid? student.errors.full_messages.each do |msg| # you can customize the error message here: errors.add_to_base(“Student Error: #{msg}”) end end end end Now @school.errors will contain the correct errors: format.xml { render … Read more

When (if) to consolidate ActiveRecord migrations?

Yes, this makes sense. There is a practice of consolidating migrations. To do this, simply copy the current schema into a migration, and delete all the earlier migrations. Then you have fewer files to manage, and the tests can run faster. You need to be careful doing this, especially if you have migrations running automatically … Read more

Rails 3 devise, current_user is not accessible in a Model ?

This doesn’t make much sense, as you already pointed. The current_user doesn’t belong to model logic at all, it should be handled on the controller level. But you can still create scope like that, just pass the parameter to it from the controller: scope :instanceprojects, lambda { |user| where(“projects.instance_id = ?”, user.instance_id) } Now you … Read more

How do i specify and validate an enum in rails?

Now that Rails 4.1 includes enums you can do the following: class Attend < ActiveRecord::Base enum size: [:yes, :no, :maybe] validates :size, inclusion: { in: sizes.keys } end Which then provides you with a scope (ie: Attend.yes, Attend.no, Attend.maybe), a checker method to see if certain status is set (ie: #yes?, #no?, #maybe?), along with … Read more

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