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
2
votes
2 answers

Rails before_destroy callback db changes always rolled back

I'm trying to prevent deletion of models from the db and pretty much follow this guide (see 9.2.5.3 Exercise Your Paranoia with before_destroy) from a Rails 4 book. I have a simple model: class User < ActiveRecord::Base before_destroy do …
2
votes
1 answer

Mount_upload on dynamic field with carrierwave

I have a model named CustomFields that belongs to a main class (that has_many :custom_fields). This model has the attributes contents and datatype. I want the contents to be either a string or a uploader using carrierwave, according to the datatype…
Ricardo Nacif
  • 508
  • 4
  • 12
2
votes
2 answers

Whats the alternative syntax for ActiveRecord::RecordNotFound in Rails 3

With the introduction of ActiveModel in Rails 3 whats the new method of calling ActiveRecord::RecordNotFound. ActiveModel::RecordNotFound ?
Rare Pleasures
  • 1,611
  • 1
  • 10
  • 5
2
votes
1 answer

How to handle date fields in a non-model Rails form?

I am creating a non-model object that will be used with a Rails form builder by using ActiveModel. This is a Rails 3 project. Here's an example of what I have so far: class SalesReport include ActiveModel::Validations include…
Andrew
  • 227,796
  • 193
  • 515
  • 708
2
votes
2 answers

Can't include ActiveModel::Model

I have a class TaskBuilder to which I want to add various ActiveModel methods so it can take advantage of ActionPack. As I understand it, in Rails 4, this will do the trick: class TaskBuilder include ActiveModel::Model When I do this, I get this…
Steve Rowley
  • 1,548
  • 1
  • 11
  • 18
2
votes
1 answer

Using ActiveModel Validations HelperMethods on the model instance?

Looking at the rails source for ActiveModel::Validations, the HelperMethods module is both included and extended in the underlying model: module Validations extend ActiveSupport::Concern included do extend ActiveModel::Callbacks extend…
Eric Hutzelman
  • 206
  • 1
  • 5
2
votes
1 answer

Can validate_uniqueness_of work with custom scopes?

I'm working on an RoR project and I'd like to have a uniqueness validation on one of my models that checks against a custom scope: class Keyword < ActiveRecord::Base belongs_to :keyword_list scope :active, -> { where("expiration > ?",…
Dylan Karr
  • 3,304
  • 4
  • 19
  • 29
2
votes
1 answer

require JUST ActiveModel::Validations in an rspec test

I have a class that I'm trying to test that includes ActiveModel::Validations module SomeModule class SomeClass include ActiveModel::Validations end end I'm trying to test it without spec_helper to keep it fast, but a simple require…
Keith Johnson
  • 730
  • 2
  • 9
  • 19
2
votes
1 answer

How to use activemodel to validates date_time_select

I am using rails 4.0 I wonder how to validate date_select with activemodel let's suppose I have the code as follow app/models/book.rb class Book include ActiveModel::Model include ActiveModel::Validations attr_accessor :title, :written_on …
Suracheth Chawla
  • 984
  • 1
  • 9
  • 20
2
votes
1 answer

ActiveModel timestamp fields: where does the timestamp come from?

In a Rail model, when a record gets added/updated, is the timestamp given per the clock on the rails server or the database server?
NJ.
  • 2,155
  • 6
  • 26
  • 35
2
votes
1 answer

Activemodel date validation and assignment in forms

I am using activemodel(Not activerecord, and I have the following code: class PaymentForm include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :agree_auto_renew, :card_owner, :address1,…
phil88530
  • 1,499
  • 3
  • 19
  • 27
2
votes
2 answers

Cannot delete record after using Rails default scope with associations

I have tables emails and users. The emails table has two fields: user_id and publish_id. I have used a default scope like this: default_scope joins('LEFT OUTER JOIN users ON users.id = emails.publish_id').where("users.status IN (?)", ["approve",…
2
votes
1 answer

When building a json api for a model with a belongs_to association, the whitelisted attributes of that association are ignored

I'm building a json api for my model, User and a User belongs_to :role. Even though I have already build a json api for Role and whitelisted the attributes I wanted to include, the whitelisted attributes are ignored whenever I visit a user#show…
2
votes
2 answers

Rails Active Model serializers not setting root

I have a class that is not an ActiveRecord object and im trying to create a AM serializer for it. I can return the proper json, but its not including a root in have this in my controller format.json { render json: @current_user, root: "current_user"…
CoderStash
  • 171
  • 3
  • 15
2
votes
1 answer

Mongoid + ActiveModel validation + I18n, not properly translating attributes

I am trying to internationalize the attributes of my Booking model From my fr.yml : attributes: &attributes booking: first_name: 'Prénom' last_name: 'Nom' email: "Email" phone: 'Téléphone' activemodel: errors: …
Intrepidd
  • 19,772
  • 6
  • 55
  • 63