activemodel

ActiveModel search by association

懵懂的女人 提交于 2019-12-12 04:08:21
问题 I've seen similar questions but nothing exactly like mine. I apologize if this is a duplicate - if it is, please refer me to answer(s). I need to search orders by customer names, and the link between the two is users. Here are my models: class Customer < ApplicationRecord belongs_to :user end class User < ApplicationRecord has_one :customer has_many :orders end class Order < ApplicationRecord belongs_to :user end I'm trying to search using: @orders = Order.joins(:user).joins(:customers).where

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

杀马特。学长 韩版系。学妹 提交于 2019-12-11 14:34:36
问题 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 or add the error to my array of errors rather than have this page show up? Controller: class UserController < ApplicationController def index end def new @user = User.new end def create @user = User.new(params[:user]) if @user.valid? user = Parse:

Customise ActiveModel full_messages

◇◆丶佛笑我妖孽 提交于 2019-12-11 03:42:15
问题 I would like to remove the attribute from my custom validation messages and just display the message, so instead of School Please Provide Your School Name I want to return Please Provide Your School Name As set in my model here validates :school, presence: { message: 'Please Provide Your School Name' } The message gets returned as JSON response. Looking at the full_messages method # File activemodel/lib/active_model/errors.rb, line 348 def full_messages map { |attribute, message| full_message

Deploying to Rails project to Heroku: Could not find activemodel

谁说我不能喝 提交于 2019-12-10 22:54:56
问题 I am relatively new to rails and have been working my way through the Michael Hartl Tutorial. Throughout I have been deploying my projects to Heroku, however now I am at the end of chapter 5, I am getting an "application error". Specifically I am getting the following errors when I look into my Heroku logs: 2012-01-04T03:05:04+00:00 app[web.1]: Could not find activemodel-3.2.0.rc1 in any of the sources 2012-01-04T03:05:06+00:00 heroku[web.1]: State changed from starting to crashed 2012-01

RSpec and ActiveModel

可紊 提交于 2019-12-10 22:35:37
问题 I have a module and it includes activemodel and I want to test it using rspec. This is my setup so far: lib/ |__ my_module/ | |__ base.rb |__ my_module.rb spec/ |__ my_module_spec.rb |__ spec_helper.rb |__ support/ |__ shared_examples/ |__ active_model.rb inside 'my_class.rb': require "active_model" require "my_module/base" inside 'base.rb': module MyModule class Base extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations end end inside 'my_module_spec.rb'

validates :something, :confirmation => true & attr_accessor confusion

馋奶兔 提交于 2019-12-10 11:38:34
问题 am struggling with Ruby validates :confirmation => true in my Rails app. Consider the following code: # == Schema Information # # Table name: things # # id :integer not null, primary key # pin :integer(8) # created_at :datetime # updated_at :datetime # class Things < ActiveRecord::Base #attr_accessor :pin attr_accessible :pin, :pin_confirmation validates :pin, :confirmation => true, :length => { :within => 7..15 }, :numericality => { :only_integer => true } end As the code is above, my

Rails 5 throw abort : how do I setup error messages?

 ̄綄美尐妖づ 提交于 2019-12-09 16:15:58
问题 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 user.destroy # => user.errors is empty Here is my model class User before_destroy :destroy_validation, if: :some_reason private def destroy_validation throw(:abort) if some_condition end 回答1: You can use errors.add for your class method. User model: def destroy_validation if some_condition errors.add(:base,

validates :something, :confirmation => true & attr_accessor confusion

北城以北 提交于 2019-12-08 20:04:25
am struggling with Ruby validates :confirmation => true in my Rails app. Consider the following code: # == Schema Information # # Table name: things # # id :integer not null, primary key # pin :integer(8) # created_at :datetime # updated_at :datetime # class Things < ActiveRecord::Base #attr_accessor :pin attr_accessible :pin, :pin_confirmation validates :pin, :confirmation => true, :length => { :within => 7..15 }, :numericality => { :only_integer => true } end As the code is above, my validation works fine from the Rails console: 1.9.3-p0 :002 > l2 = Thing.create! :pin => 1234567, :pin

Validate date and time fields together in rails model

橙三吉。 提交于 2019-12-08 07:44:28
EDIT: In case the question below looks a bit 'extensive', the summary is that I just want to combine a date field and an optional time field in my model purely for the purpose of validating it using a date validator but I can't get my test to fail correctly when I pass in a string as the time. EDIT 2: Since I'm still struggling to find a way to join a date object with a time object for the purpose of validation then I wondered if I could just validate that the time value was a Time object using is_a?(Time) (rather than an invalid string) but that didn't seem to work either and I wondered if it

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

不问归期 提交于 2019-12-07 04:25:04
问题 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 create or edit model data. Take for example a form with inputs to control the extraction and display of some statistics. What does rails provide me with for validating the user input of this form, which won't be invoking save on any records?