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
20
votes
1 answer

How to generate associations between models

I am wondering how to do the association in Rails correct. First I create a City model and an Organisation. Now I want to have an Organisation have a City... this is done by adding the has_many and has_one associations. After that I run rake…
Mark
  • 7,507
  • 12
  • 52
  • 88
20
votes
3 answers

Efficient way to report record validation warnings as well as errors?

I've got a Rails project where, as in most apps, we have a number of hard-and-fast validation rules to which all objects must conform before being persisted. Naturally, ActiveModel's Validations are perfect for that – we're using a combination of…
ScottM
  • 7,108
  • 1
  • 25
  • 42
19
votes
2 answers

Getting actual array of results using Mongoid

With a regular ActiveRecord/SQL setup in Rails, in console when I execute commands *.where, *.all etc., I get back the actual array of record items. However, after switching to Mongoid, I instead get back a criteria. How do I get the actual…
Newy
  • 38,977
  • 9
  • 43
  • 59
18
votes
6 answers

rails 3:how to generate models for existing database tables

I've configured my database.yml to point to my existing mysql database how can I generate models from it? rails generate model existing_table_name only gives an emty model..
dark_ruby
  • 7,646
  • 7
  • 32
  • 57
18
votes
2 answers

Rails 5 throw abort : how do I setup error messages?

Rails has introduced this throw(:abort) syntax, but now how do I get meaningful destroy errors ? For validation errors one would do if not user.save # => user.errors has information if not user.destroy # => user.errors is empty Here is my…
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
18
votes
1 answer

What is the ActiveModel method attribute "_was" used for?

When using autocomplete in the console, I often see "_was" postpended to my attributes. But I can't find any documentation or best practices for usage. What does it do and how should it be used? Example: user.fname has the method…
beeudoublez
  • 1,222
  • 1
  • 12
  • 27
17
votes
6 answers

How can i set include_root_in_json to false for all my RoR models?

How can i set include_root_in_json to false for all my RoR models? I've tried to set ActiveModel::Base.include_root_in_json = false inside application.rb, but it has no effect.
Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197
17
votes
1 answer

Where are Default Validation Error Messages in Rails 3.0?

Where are the default validation error messages in Rails 3.0? What is the equivalent of ActiveRecord::Error.default_error_messages[:taken], for example? I have gotten as far as finding that ActiveModel handles the errors rather than ActiveRecord,…
Mike Blyth
  • 4,158
  • 4
  • 30
  • 41
17
votes
2 answers

ActiveModel::Validations on anonymous class

I'm working on a small DataMapper-like ODM project, and I'm trying to make use of the ActiveModel::Validations component. However, I ran into a problem while writing tests - I'm using anonymous classes to construct my test schemas, however when it…
motns
  • 305
  • 2
  • 6
15
votes
1 answer

How to pass argument to delegate method in Rails

I would like to have a Dashboard to display summary of multiple models, and I implemented it using Presenter without its own data. I use an ActiveModel class (without data table): class Dashboard attr_accessor :user_id def initialize(id) …
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
15
votes
2 answers

ActiveModel - View - Controller in Rails instead of ActiveRecord?

I'm trying to use ActiveModel instead of ActiveRecord for my models because I do not want my models to have anything to do with the database. Below is my model: class User include ActiveModel::Validations validates :name, :presence => true …
Bilal Wahla
  • 665
  • 1
  • 12
  • 28
14
votes
3 answers

Rails, using the dirty or changed? flag with after_commit

I heard rails has a dirty/change flag. Is it possible to use that in the after_commit callback? In my user model I have: after_commit :push_changes In def push_changes I would like a way to know if the name field changed. Is that possible?
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
14
votes
2 answers

on an ActiveModel Object, how do I check uniqueness?

In Bryan Helmkamp's excellent blog post called "7 Patterns to Refactor Fat ActiveRecord Models", he mentions using Form Objects to abstract away multi-layer forms and stop using accepts_nested_attributes_for. Edit: see below for a solution. I've…
JeanMertz
  • 2,250
  • 2
  • 21
  • 26
14
votes
2 answers

What is the difference between ActiveResource and ActiveModel?

As a preface to this question: I am brand new to Rails development (and web-development in general), and some of my concerns may be unfounded, so any feedback would be very helpful. I am attempting to make a Rails application that plugs into a…
finiteloop
  • 4,444
  • 8
  • 41
  • 64
13
votes
1 answer

Multiple HABTM properties with ActiveAdmin and Rails 4: data not saved

I have the following models: class Programme < ActiveRecord::Base has_and_belongs_to_many :nationalities, class_name: 'Nation', join_table: 'nationalities_nations' has_and_belongs_to_many :destinations, class_name: 'Nation', join_table:…
Besi
  • 22,579
  • 24
  • 131
  • 223
1
2
3
47 48