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

Validation of Month and Year

I'm using a custom validation to check if a card has expired (it has a month / year). Here's what I've got: validate :card_not_expired def card_not_expired if exp_year > Time.now.year or (exp_year == Time.now.year and…
FloatingRock
  • 6,741
  • 6
  • 42
  • 75
0
votes
1 answer

How to associate ActiveRecord model and plain class model

class User < ActiveRecord::Base has_one :report has_many :invoices end class Report include ActiveModel::Model belongs_to :user def self.monthly_sales user.invoices.group_by { |i| i.date.beginning_of_month } end end Unfortunately…
turhanco
  • 941
  • 10
  • 19
0
votes
2 answers

How to save attributes using Form Objects in Rails

I have a user model which consists of 8-10 attributes. I tried to use form object concept to extract out the validations stuffs into another UserForm Class. FYI I am using Rails 4 :) My controller : class UsersController < ApplicationController …
Ajay
  • 4,199
  • 4
  • 27
  • 47
0
votes
1 answer

Rails loses change tracking when calling instance method

When changing a model's attributes in the controller's update, I want to generate a history of changes that are about to be made. For that, I've created a method generate_history to access from the instance. The class: class Assist <…
0
votes
1 answer

Load model partly in Ruby On rails

guys! I'm trying to make right architecture decision: I need to hide from user of my site some fields of other users by default. So not to bother with filtering the fields in views I want to not load those fields at all, like: default_scope -> {…
Roaring Stones
  • 1,054
  • 7
  • 22
0
votes
1 answer

Circular dependency detected while autoloading constant (ActiveRecord)

I have manually specified the relationships in the models (haven't found a way how to automatically generate them from a ERD model or an existing database) and than tried to generate a migration containing the FKs using the immigrant gem. I am…
0
votes
1 answer

Rails ActiveModel::ForbiddenAttributesError Editing multiple records

I followed Railscast Episode 165 (revised) to the tee on how to edit and Update multiple records in one form. But when I submit my form to edit multiple records at the same time, I get: ActiveModel::ForbiddenAttributesError For this…
Devin
  • 1,011
  • 2
  • 14
  • 30
0
votes
1 answer

What Is Best Practice For Ensuring Associations Exist For Nested Forms?

When using a form that contains nested resources it is necessary to ensure that the nested resources exist for the form builder to work appropriately. I have seen this achieved in numerous ways – building them in the controller or building them in…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
0
votes
1 answer

ActiveModel presence validation fails

I have created a model (called PhoneNumber) that is not backed by a database table: class PhoneNumber include ActiveModel::Validations attr_accessor :pnumber validates :pnumber, presence: true, on: :create #=> { :message => " cannot be…
Nick
  • 2,924
  • 4
  • 36
  • 43
0
votes
1 answer

How can I disguise the ID part of my URL in rails when working without active record?

I have a URL that looks like this: http://localhost:3000/activities/5NcsdzWvXbv I'd like to make it look something like this: http://localhost:3000/activities/river-rafting I have a column in my non-activerecord database that stores the names of…
LondonGuy
  • 10,778
  • 11
  • 79
  • 151
0
votes
1 answer

Rails way to put specific item at the beginning of object order in query

I want to take the current Profile that's set under User and move it to the front of the ordered list. Is there a way to do this in a model scope, or even a specific query? class Profile belongs_to :profileable, polymorphic: true end class User …
6ft Dan
  • 2,365
  • 1
  • 33
  • 46
0
votes
1 answer

uninitialized constant Order::OrderItem with belongs_to in OrderItem

I have a code that once i create the order, it will automatically fill up information for the order items (because every order has many items) but i have the error uninitialized constant Order::OrderItem. How do i solve this? Model class Order <…
0
votes
1 answer

Is it possible to catch ruby on rails error pages and do my own error handling?

I get this page when incorrect form login details are entered: When credentials are correct the user is just logged in. When they're invalid this error page comes up. How do I catch this page and handle the error myself? E.G. redirect to same page…
LondonGuy
  • 10,778
  • 11
  • 79
  • 151
0
votes
1 answer

undefined method `i18n_key' for Address:Class

I get this error when I load a page that contains a form (like edit.html.erb or new.html.erb). It freezes on the labels. I need to underline that I am not using ActiveRecord as an ORM in my app but Perpeuity gem which is an implementation of the…
evoo
  • 300
  • 4
  • 15
0
votes
2 answers

I'm getting an undefined method with a form_for when method does exist in ruby on rails

I'm debugging a variable from my controller in a view and it's showing me what I expect to see which is an object with properties: --- !ruby/hash:Parse::Object alternativeColour: false collection: 1 colour: 2 favourite: false gender: 2 image:…
LondonGuy
  • 10,778
  • 11
  • 79
  • 151