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
13
votes
4 answers

Active Model Serializers: Adding extra information outside root in ArraySerializer

Say I have a model User and a serializer UserSerializer < ActiveModel::Serializer, and a controller that looks like this: class UsersController < ApplicationController respond_to :json def index respond_with User.all end end Now if I…
GMA
  • 5,816
  • 6
  • 51
  • 80
13
votes
2 answers

Use ActiveModel::Serializers to include two parent json arrays

I'm trying to send my front-end application json that looks like this: { facilities: [ {id: 5, name: 'happy days ranch', location: { address: '1424 Pastoral Lane', zipcode: '25245'}, instructor_ids: [2, 4, 9]} ], instructors: [ {id:…
13
votes
1 answer

Rails/ActiveModel passing arguments to EachValidator

I have a very generic validator and I want to pass it arguments. Here is an example model: class User include Mongoid::Document field :order_type has_many :orders, inverse_of :user validates: orders, generic: true #i want to pass argument…
GTDev
  • 5,488
  • 9
  • 49
  • 84
12
votes
1 answer

What's the correct way to make before_validation, etc. work in an ActiveModel

Should I extend or include ActiveModel:Validations:Callbacks:ClassMethods or ActiveModel:Validations:Callbacks?
K Everest
  • 1,565
  • 5
  • 15
  • 23
12
votes
5 answers

ActiveModel attributes

How do I get ActiveRecord attributes method functionality? I have this class: class PurchaseForm include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :name, :surname, …
zigomir
  • 985
  • 2
  • 15
  • 30
12
votes
2 answers

Rails 3, RSpec 2.5: Using should_receive or stub_chain with named scopes

I use Rails 3.0.4 and RSpec 2.5. In my controllers I use named scopes heavily, for example @collection = GuestbookEntry.nonreplies.bydate.inclusive.paginate( :page => params[:page], :conditions => { ... }) In my tests, I want to be able…
Jens
  • 1,386
  • 14
  • 31
12
votes
3 answers

Translate model nested attribute validation message

I have a nested attribute on which I perform presence validation. I am trying without success to provide translations for the attribute name that is returned in the full error message text. The model is called Identity and contains an attribute…
starfry
  • 9,273
  • 7
  • 66
  • 96
12
votes
2 answers

How to access params in the callback of a Rails model?

I have a callback of a model that needs to create a dependent object based on another field entered in the form. But params is undefined in the callback method. Is there another way to access it? What's the proper way to pass a callback method…
at.
  • 50,922
  • 104
  • 292
  • 461
11
votes
3 answers

form_for non-AR model - fields_for Array attribute doesn't iterate

I'm having trouble getting fields_for to work on an Array attribute of a non-ActiveRecord model. Distilled down, I have to following: models/parent.rb class Parent extend ActiveModel::Naming include ActiveModel::Conversion include…
Chowlett
  • 45,935
  • 20
  • 116
  • 150
11
votes
2 answers

" 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path " error in micropost model

When I attempt to view the user profile page, I get the error above. Here's my show.html.erb code: <% provide(:title, @user.name) %>
Andrew
  • 3,839
  • 10
  • 29
  • 42
11
votes
3 answers

Serialize permissions (e.g. CanCan) with active_model_serializers

How do I serialize permissions with active_model_serializers? I don't have access to current_user or the can? method in models and serializers.
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
10
votes
3 answers

Is Mongoid::Document a GlobalID::Identification for ActiveJobs?

According to the ActiveJobs guide, section 8, it says: This works with any class that mixes in GlobalID::Identification, which by default has been mixed into Active Model classes. Mongoid::Document mixes ActiveModel::Model, but I can't find…
Jaffa
  • 12,442
  • 4
  • 49
  • 101
9
votes
4 answers

How to create ActiveRecord tableless Model in Rails 5?

I tried create new model which has auto type casting without table in database. I've tried to inherit from ActiveRecord::Base it's thrown exception ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "people" does not exist Class…
Bajorek
  • 319
  • 1
  • 2
  • 8
9
votes
2 answers

Rails 3.2 to 4.0 Upgrade: Undefined method to_datetime for false:FalseClass

I'm upgrading a Rails application I've inherited from 3.2 to 4.0.1. I followed and finished the edge guide here: http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-3-2-to-rails-4-0 I've gotten everything fixed…
9
votes
1 answer

Change or update an attribute value during Rails ActiveRecord validation

Summary: I'm trying to alter an attribute's value within a custom ActiveModel::EachValidator validator. Given the following prototype: def validate_each(record, attribute, value) trying to set value = thing doesn't appear to do anything -- am I…
aardvarkk
  • 14,955
  • 7
  • 67
  • 96
1 2
3
47 48