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

Rails: In Rails how to use the model’s Attribute API on a tableless model

I have a table-less model like this: class SomeModel include ActiveModel::Model attribute :foo, :integer, default: 100 end I’m trying to use an attribute from the link below, it works perfectly in normal models however I cannot get it to work…
CafeHey
  • 5,699
  • 19
  • 82
  • 145
6
votes
1 answer

ActiveModel::Dirty and JSON fields

I am using ActiveModel::Dirty to track changes made in a form. Now everything is working as I expect. With stuff like phone numbers that get dashes in them when in the form I simply format them and then the phone number will not appear in the list…
Gabriel
  • 621
  • 7
  • 19
6
votes
1 answer

FactoryGirl - override association with trait?

Let's say I have the following ActiveRecord models: class Car belongs_to :driver end class Driver # Has attribute :name has_one :car end And I define a couple of factories using these models: FactoryGirl.define do factory :car do …
Argus9
  • 1,863
  • 4
  • 26
  • 40
6
votes
3 answers

Using `assign_attributes` saves `has_many through:` association immediately

As far as I know, assign_attributes (unlike update_attributes) is not supposed to save the record or for that matter, any record. So it quite startled me when I discovered that this is not true when supplying _ids for a has_many through:…
sudoremo
  • 2,274
  • 2
  • 22
  • 39
6
votes
2 answers

ActiveModel without rails

Is possible use ActiveModel without rails?. Is possible use ActiveModel in desktop application?
yeraycaballero
  • 1,603
  • 3
  • 19
  • 29
6
votes
1 answer

How to properly use joins on polymorphic association in Active Record

I have a Model Action that has a belongs_to :actor, polymorphic: true that actor can be: a Customer, Admin, Seller or Guest. I want to filter instances of Action to just the actions made by a particular Seller or Guest. How can I do that? In a…
ciaoben
  • 3,138
  • 4
  • 27
  • 42
6
votes
0 answers

Rails 4 ActiveModel validation of nested attribute (without ActiveRecord)

I am working on a small Rails project that simply allows a user to submit a form and the information from the form is sent via an API call to another service for consumption. Here is my model: class ServiceRequest include ActiveModel::Model …
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
6
votes
2 answers

rails serialized array validation

I need to validate email saving in email_list. For validation I have EmailValidator. But I cannot figure out how to use it in pair of validates_each. Are there any other ways to make validations? class A < ActiveRecord::Base serialize :email_list …
gayavat
  • 18,910
  • 11
  • 45
  • 55
6
votes
1 answer

Rails - changing default param_key for a model

I've been using plain Ruby form objects in rails, but to keep my code organized, I've ended up having to add a ton of namespaces to them. So I'll have a form like: class User::Registration::NewForm extend Forwardable extend ActiveModel::Naming …
Bryce
  • 2,802
  • 1
  • 21
  • 46
5
votes
2 answers

Rails 3: Display validation errors for a form (not saving an ActiveRecord model)

Apologies if this is a really common and/or ridiculous question; I swear I've read over the documentation multiple times and everything seems so focused on ActiveRecord to the point they've wandered off the path of forms that do something other than…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
5
votes
1 answer

Max number for ActiveModel::Type::Integer

I'm trying to store the number 3980040429 as an ID for an object, but I'm etting the following error: ActiveModel::RangeError (3980040429 is out of range for ActiveModel::Type::Integer with limit 4 bytes) Doesn't the 4 bytes equal to 4 294 967…
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
5
votes
1 answer

Ransack association search breaking after upgrading rails

I have a model, Student, that with a habtm relationship with another model, Group. The following code: Student.ransack(groups_id_eq: 22839).result Produces the following SQL: SELECT "students".* FROM "students" LEFT OUTER JOIN "groups_students" ON…
Will I AM
  • 220
  • 1
  • 9
5
votes
1 answer

NoMethodError (undefined method `empty?' for #):

I created a rails API using postgre database in which i have a model (table) name as counseling_event NOTE: Not using scaffold and using rails-5 My scheman(migration) for counseling_event is as follow: class CreateCounselingEvent <…
5
votes
3 answers

Passing options to ActiveModel serializer

When using the serializer from the controller I can pass extra options to it like so render json: user, some_option: 'foobar Then I can reference some_option within the serializer as serialization_options[:some_option] But, if I call the…
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
5
votes
3 answers

Rails 3: Custom error message in validation

I don't understand why the following is not working in Rails 3. I'm getting "undefined local variable or method `custom_message'" error. validates :to_email, :email_format => { :message => custom_message } def custom_message self.to_name + "'s…
Vincent
  • 16,086
  • 18
  • 67
  • 73