activemodel Questions

4

Solved

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 wh...
Chelseachelsey asked 29/6, 2017 at 7:57

3

Solved

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) %> <div class="row"> <aside class="span4"> ...
Airtoair asked 2/12, 2013 at 4:42

8

I have a Rails 3.0.9 app that, once it is deployed, suffers from a bunch of ActiveModel::MissingAttributeErrors that crop up causing 500s. The errors occur fairly randomly, sometimes a page will lo...
Excrete asked 25/7, 2011 at 17:44

7

Solved

I have the following validator: # Source: http://guides.rubyonrails.org/active_record_validations_callbacks.html#custom-validators # app/validators/email_validator.rb class EmailValidator < Ac...
Gumwood asked 12/10, 2011 at 17:38

5

Solved

Let's say you're implementing a REST API in Rails. When serving a collection, you might want to only include a few attributes: /people But when serving a single resource, you want to include all...
Alonaalone asked 18/9, 2012 at 21:32

5

Solved

How cat I get list of validations defined in model Example: class ModelName validates_presence_of :field_name validates_inclusion_of :sex, :in => %w(M F) end I need Hash like: {:field_nam...
Deceive asked 29/10, 2010 at 12:33

8

Solved

The #new_record? function determines if a record has been saved. But it is always false in the after_save hook. Is there a way to determine whether the record is a newly created record or an old on...
Kleper asked 11/2, 2011 at 23:30

1

Solved

I have a rails model that validates the uniqueness of order_number value, thay should start from 1_000_000, so I added a variable that is used as a first value: # order model STARTING_NUMBER = 1_0...
Thracian asked 25/9, 2021 at 5:42

8

Solved

I would like to know if it is possible to get the types (as known by AR - eg in the migration script and database) programmatically (I know the data exists in there somewhere). For example, I can...
Familist asked 8/3, 2010 at 5:57

7

Solved

If I try to execute the following code: hassle = rota.hassles.create(:sender => user1, :receiver => user2, :type => "sms") I obain the following error: Failure/Error: hassle = rota.has...
Solorzano asked 13/5, 2012 at 20:25

3

Solved

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 kno...
Putrescine asked 23/8, 2011 at 1:11

4

Solved

I'm looking for a way in the rails console to find any values that finish with a particular string and remove that string from the value. Something along the lines of: Model.all.each{|x| x.durati...
Addington asked 19/4, 2016 at 7:9

5

Solved

I have a rails model which has 7 numeric attributes filled in by the user via a form. I need to validate the presence of each of these attributes which is obviously easy using validates :attribut...
Lampert asked 11/5, 2011 at 14:38

3

Solved

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 ar...
Palate asked 8/7, 2014 at 9:44

3

Solved

I have the following model: class Contact attr_accessor :name, :emails, :message def initialize(attrs = {}) attrs.each do |k, v| self.send "#{k}=", v end end def persisted? false end en...
Extinctive asked 13/2, 2013 at 19:44

5

Solved

I am trying to send a notification email in my rails app only if the value of my column status was modified by the current update. I tried using Active Model Dirty as was suggested in some post and...
Spendable asked 30/8, 2016 at 18:24

2

Solved

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 ...

4

Solved

I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like: class Foo < ActiveRecord::Base attr_accessor :bar end I would like to be ab...
Appalachian asked 14/4, 2011 at 16:54

1

Solved

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...
Gorgonzola asked 26/11, 2018 at 22:34

4

Solved

In my app; when I submit form, I get this error: LoadError at /questions Unable to autoload constant Message, expected /app/models/message.rb to define it It points to the create action in the ...
Dissimilar asked 20/6, 2014 at 16:35

4

Solved

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...

3

Solved

I have the following models: User (id, name, network_id) Network(id, title) What kind of Rails model assoc do I need to add so that I can do: @user.network.title @network.users Thanks
Bobbobb asked 18/12, 2011 at 21:49

3

Solved

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 i...
Batavia asked 25/11, 2011 at 16:37

2

Solved

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 u...
Lamberto asked 28/7, 2016 at 0:28

1

Solved

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...
Firewarden asked 22/8, 2017 at 17:25

© 2022 - 2024 — McMap. All rights reserved.