activemodel

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes

半城伤御伤魂 提交于 2019-11-30 06:30:08
问题 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.hassles.create(:sender => user1, :receiver => user2, :type => "sms") ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: type I am not sure what this means. I have made the :type to be compulsory, so if I do remove it, I get an sql error. 回答1: A couple things: Mass Assignment usually

ActiveModel::MissingAttributeError occurs after deploying and then goes away after a while

穿精又带淫゛_ 提交于 2019-11-30 02:40:00
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 load, other times it won't, but the attributes are all existing attributes in the database and should be found. The strange part is that after a while, the errors go away. Suddenly, they stop causing an issue. I have searched about for a solution to this, but this error mostly occurs either when someone has done Model.all(:select => 'column_x,column_y') and are calling for column_z or when they are

Where are Default Validation Error Messages in Rails 3.0?

匆匆过客 提交于 2019-11-30 00:50:29
问题 Where are the default validation error messages in Rails 3.0? What is the equivalent of ActiveRecord::Error.default_error_messages[:taken], for example? I have gotten as far as finding that ActiveModel handles the errors rather than ActiveRecord, but I can't find the errors themselves. 回答1: http://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml and http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml :D UPDATE: Maybe you should

get validations from model

戏子无情 提交于 2019-11-29 20:52:47
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'} 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) This code yields an array of required fields. It should be adaptable to your needs. @required_fields = [] ModelName.validators.each do |v| @required_fields << v

How to determine if a record is just created or updated in after_save

不羁的心 提交于 2019-11-29 20:32:05
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 one from update? I'm hoping not to use another callback such as before_create to set a flag in the model or require another query into the db. Any advice is appreciated. Edit: Need to determine it in after_save hook, and for my particular use case, there is no updated_at or updated_on timestamp I was looking to use this for an after_save callback. A simpler solution is to use id_changed? (since it won

on an ActiveModel Object, how do I check uniqueness?

别说谁变了你拦得住时间么 提交于 2019-11-29 07:10:08
问题 In Bryan Helmkamp's excellent blog post called "7 Patterns to Refactor Fat ActiveRecord Models", he mentions using Form Objects to abstract away multi-layer forms and stop using accepts_nested_attributes_for . Edit: see below for a solution. I've almost exactly duplicated his code sample, as I had the same problem to solve: class Signup include Virtus extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations attr_reader :user attr_reader :account attribute

How to pass argument to delegate method in Rails

倾然丶 夕夏残阳落幕 提交于 2019-11-29 06:00:58
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).address . If Dashboard was an ActiveRecord class, then I could have declared Dashboard#belongs_to :account

Serialize permissions (e.g. CanCan) with active_model_serializers

╄→尐↘猪︶ㄣ 提交于 2019-11-29 01:33:30
问题 How do I serialize permissions with active_model_serializers? I don't have access to current_user or the can? method in models and serializers. 回答1: First, to get access to the current_user in the serializer context, use the new scope feature: class ApplicationController < ActionController::Base ... serialization_scope :current_user end In case you are instantiating serializers manually, be sure to pass the scope: model.active_model_serializer.new(model, scope: serialization_scope) Then

rails 3:how to generate models for existing database tables

邮差的信 提交于 2019-11-28 19:33:19
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.. 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 though, but if your database follows Rails conventions they are easily added. Update I've noticed this

How to test a custom validator?

南笙酒味 提交于 2019-11-28 19:08:47
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 so far is I am not sure how to initialize an EachValidator . Here's a quick spec I knocked up for that