Questions tagged [activemodel]

A toolkit for building modeling frameworks like Active Record. Rich support for attributes, callbacks, validations, serialization, internationalization, and testing.

ActiveModel brings many of ActiveRecord's features (such as validations and callbacks) to non-ActiveRecord classes.

707 questions
119
votes
8 answers

How to determine if a record is just created or updated in after_save

The #new_record? function determines if a record has been saved. But it is always false in the after_save hook. Is there a way to determine whether the record is a newly created record or an old one from update? I'm hoping not to use another…
Aaron Qian
  • 4,477
  • 2
  • 24
  • 27
79
votes
8 answers

Getting types of the attributes in an ActiveRecord object

I would like to know if it is possible to get the types (as known by AR - eg in the migration script and database) programmatically (I know the data exists in there somewhere). For example, I can deal with all the attribute names: …
Michael Neale
  • 19,248
  • 19
  • 77
  • 109
71
votes
3 answers

Difference between Active Model, Active Record and Active Resource

Is there anyone who can help me by defining the exact difference between Active Model, Active Record and Active Resource. I have done enough googling in order to find the exact difference, but didn't get anything concrete which can tell the exact…
64
votes
4 answers

LoadError Unable to autoload constant Message

In my app; when I submit form, I get this error: LoadError at /questions Unable to autoload constant Message, expected /app/models/message.rb to define it It points to the create action in the Questions controller: @message =…
pwz2000
  • 1,385
  • 2
  • 16
  • 50
58
votes
7 answers

ActiveModel::MissingAttributeError occurs after deploying and then goes away after a while

I have a Rails 3.0.9 app that, once it is deployed, suffers from a bunch of ActiveModel::MissingAttributeErrors that crop up causing 500s. The errors occur fairly randomly, sometimes a page will load, other times it won't, but the attributes are all…
philnash
  • 70,667
  • 10
  • 60
  • 88
54
votes
5 answers

Controlling the order of rails validations

I have a rails model which has 7 numeric attributes filled in by the user via a form. I need to validate the presence of each of these attributes which is obviously easy using validates :attribute1, :presence => true validates :attribute2, :presence…
David Tuite
  • 22,258
  • 25
  • 106
  • 176
50
votes
5 answers

get validations from model

How cat I get list of validations defined in model Example: class ModelName validates_presence_of :field_name validates_inclusion_of :sex, :in => %w(M F) end I need Hash like: {:field_name => 'required', :sex => 'Must be in: M, F'}
manzhikov
  • 3,778
  • 3
  • 20
  • 22
47
votes
5 answers

How to implement multiple different serializers for same model using ActiveModel::Serializers?

Let's say you're implementing a REST API in Rails. When serving a collection, you might want to only include a few attributes: /people But when serving a single resource, you want to include all the attributes: /people/1 I don't see how to do…
odigity
  • 7,568
  • 4
  • 37
  • 51
45
votes
7 answers

How to test a custom validator?

I have the following validator: # Source: http://guides.rubyonrails.org/active_record_validations_callbacks.html#custom-validators # app/validators/email_validator.rb class EmailValidator < ActiveModel::EachValidator def validate_each(object,…
GTDev
  • 5,488
  • 9
  • 49
  • 84
44
votes
5 answers

Forbidden Attributes Error in Rails 4 when encountering a situation where one would have used attr_accessible in earlier versions of Rails

With the recent upgrade to Rails 4, updating attributes using code resembling the below does not work, I get a ActiveModel::ForbiddenAttributes error: @user.update_attributes(params[:user], :as => :admin) Where User has the following…
28
votes
3 answers

How to setup a one to many relationship?

I have the following models: User (id, name, network_id) Network(id, title) What kind of Rails model assoc do I need to add so that I can do: @user.network.title @network.users Thanks
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
28
votes
4 answers

Track dirty for not-persisted attribute in an ActiveRecord object in rails

I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like: class Foo < ActiveRecord::Base attr_accessor :bar end I would like to be able to track changes to 'bar', with methods like…
Santiago Palladino
  • 3,522
  • 2
  • 26
  • 36
27
votes
3 answers

Shared scopes via module?

I want to DRY up several models by moving shared scopes into a module, something like: module CommonScopes extend ActiveSupport::Concern module ClassMethods scope :ordered_for_display, order("#{self.to_s.tableize}.rank asc") end end I…
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
26
votes
7 answers

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes

If I try to execute the following code: hassle = rota.hassles.create(:sender => user1, :receiver => user2, :type => "sms") I obain the following error: Failure/Error: hassle = rota.hassles.create(:sender => user1, :receiver => user2, :type =>…
Karan
  • 14,824
  • 24
  • 91
  • 157
21
votes
3 answers

where to put ActiveModel::Validator?

I try to follow http://api.rubyonrails.org/classes/ActiveModel/Validator.html , but where should I put the class MyValidator < ActiveModel::Validator def validate(record) if some_complex_logic record.errors[:base] = "This record…
wizztjh
  • 6,979
  • 6
  • 58
  • 92
1
2 3
47 48