activemodel

get validations from model

[亡魂溺海] 提交于 2019-11-28 17:05:07
问题 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_name => 'required', :sex => 'Must be in: M, F'} 回答1: You don't need a plugin for basic needs. You can do this to get a hash of all validators. ModelName.validators If you want to get the validators for a specific field : ModelName.validators_on(:attribute) 回答2: This code yields an array of required fields. It

Forbidden Attributes Error in Rails 4 when encountering a situation where one would have used attr_accessible in earlier versions of Rails

喜你入骨 提交于 2019-11-28 06:44:11
With the recent upgrade to Rails 4, updating attributes using code resembling the below does not work, I get a ActiveModel::ForbiddenAttributes error: @user.update_attributes(params[:user], :as => :admin) Where User has the following attr_accessible line in the model: attr_accessible :role_ids, :as =>admin # or any attribute other than :role_ids contained within :user How do you accomplish the same task in Rails 4? Rails 4 now has features from the strong_parameters gem built in by default. One no longer has to make calls :as => :admin , nor do you need the attr_accessible :user_attribute, :as

How to implement multiple different serializers for same model using ActiveModel::Serializers?

五迷三道 提交于 2019-11-28 03:20:29
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 the attributes: /people/1 I don't see how to do that using ActiveModel::Serializers, since the examples all use the pattern of defining one serializer per model (with a standard naming convention) and having AMS automatically use the right one in the controller when you do: render json: @people or: render json: @person To avoid mixing view concerns into your models (via serialized variations), use the

How to pass argument to delegate method in Rails

拜拜、爱过 提交于 2019-11-27 23:32:16
问题 I would like to have a Dashboard to display summary of multiple models, and I implemented it using Presenter without its own data. I use an ActiveModel class (without data table): class Dashboard attr_accessor :user_id def initialize(id) self.user_id = id end delegate :username, :password, :to => :user delegate :address, :to => :account delegate :friends, :to => :friendship end By delegate, I want to be able to call Dashboard.address and get back Account.find_by_user_id(Dashboard.user_id)

Controlling the order of rails validations

两盒软妹~` 提交于 2019-11-27 12:33:29
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 :attribute1, :presence => true validates :attribute2, :presence => true # and so on through the attributes However I also need to run a custom validator which takes a number of the attributes and does some calculations with them. If the result of these calculations is not within a certain range then the model should be declared invalid. On it's own, this too is easy validate :calculations_ok? def calculations

rails 3:how to generate models for existing database tables

不打扰是莪最后的温柔 提交于 2019-11-27 12:23:09
问题 I've configured my database.yml to point to my existing mysql database how can I generate models from it? rails generate model existing_table_name only gives an emty model.. 回答1: A Rails model doesn't show your fields, but you can still use them. Try the following. Assuming you have a Model named ModelName and a field called "name", fire up the Rails console and type: ModelName.find_by_name('foo') Given a name that exists in the DB, you should see results. Rails doesn't infer relationships

How to test a custom validator?

我只是一个虾纸丫 提交于 2019-11-27 11:58:39
问题 I have the following validator: # Source: http://guides.rubyonrails.org/active_record_validations_callbacks.html#custom-validators # app/validators/email_validator.rb class EmailValidator < ActiveModel::EachValidator def validate_each(object, attribute, value) unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i object.errors[attribute] << (options[:message] || "is not formatted properly") end end end I would like to be able to test this in RSpec inside of my lib directory. The problem

Forbidden Attributes Error in Rails 4 when encountering a situation where one would have used attr_accessible in earlier versions of Rails

青春壹個敷衍的年華 提交于 2019-11-27 01:28:03
问题 With the recent upgrade to Rails 4, updating attributes using code resembling the below does not work, I get a ActiveModel::ForbiddenAttributes error: @user.update_attributes(params[:user], :as => :admin) Where User has the following attr_accessible line in the model: attr_accessible :role_ids, :as =>admin # or any attribute other than :role_ids contained within :user How do you accomplish the same task in Rails 4? 回答1: Rails 4 now has features from the strong_parameters gem built in by

Controlling the order of rails validations

流过昼夜 提交于 2019-11-26 15:57:38
问题 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 :attribute1, :presence => true validates :attribute2, :presence => true # and so on through the attributes However I also need to run a custom validator which takes a number of the attributes and does some calculations with them. If the result of these calculations is not within a certain range then the model