How do I stop ActiveRecord looking for a table?
I can think of a few reasons you might want to do something like this. Perhaps you want to leverage some of the non-db-related methods on ActiveRecord or you want to pass your object to something that expects and ActiveRecord instance. Without more info, it is impossible to say whether the choice to use AR here is correct or incorrect.
In any event, if you want to continue on this path...
check out this Railscast
http://railscasts.com/episodes/121-non-active-record-model
and also checkout this gem:
http://github.com/kennethkalmer/activerecord-tableless-models/tree/master
ActiveRecord validations + a DRY way to display errors?
1 has_one_attached
enables a method attached?
. You can define your own validation method, that uses this to perform validation. ActiveStorage (where the has_one_attached
comes from) does not support validations on its own yet. It will in Rails 6 but I assume you are not using this version of RAILS. You could even move the validations to an outside validator and use that to validate the file presence, size, type etc. https://guides.rubyonrails.org/active_record_validations.html#validates-with
Moving validations from the model is usually considered good practice in the long term.
2 Try to use redirect when there are errors, the same way you do it when form succedes (instead of render).
3 Yes, you can just move the part responsible for displaying errors into a partial that's easy to access across the application, and render it in the form under a condition of having errors (or render it always, but it won't display anything in it if there are no errors)
Also when submitting a question, try to specify what exactly is not working, the way you described validation not working is a bit vague.
Also on a personal note, I suggest using haml instead of erb. But that's only cause you mentioned you wanted some suggestions ;)
Validating a Rails model post-save?
There's a bug with accepts_nested_attributes_for. Meaning you have to be a little more devious when it comes to validations in the parent model.
You could use an :after_save callback in each of your nested models to check if it's the last one. But if there's many nested associations where you want to ensure at least one, this isn't very DRY.
This is however a valid workaround for the linked bug:
class Whatever < ActiveRecord::Base
:has_many => :association_a
:has_many => :association_b
def ensure_minimum_associations
bad_associations = [:association_a, :association_b].
select{|assoc| self.send(assoc).all?{|a| a.marked_for_destruction?}}
unless bad_associations.empty?
bad_associations.each do |association|
errors.add_to_base "Each #{self.class.name.downcase} must retain at least one #{association}"
end
return false
end
end
end
Add validation to prevent out of range for ActiveRecord::Type::Integer with limit 4
The max value of a 4-bytes integer column in a database is 2**31 - 1 == 2147483647
.
Fo ensure that you cannot write values bigger than that to the database just change your validation to:
validates :duration, numericality: { only_integer: true,
greater_than: 0,
less_than: 2**31 }
But looking at your stack trace it feels like the issue is not that you are trying to store a value into the database but instead, you build a query that includes a value that is over that limit and therefore your database complains about an invalid filter.
A workaround might be to limit the maximum duration in the filter by changing this line
duration = filters[:duration][:durations][index]
to
duration = [filters[:duration][:durations][index], (2**31 - 1)].min
RSpec and active record validations
You should use expect(subject).to be_valid
in the other 2 test cases. You are getting the error because you are trying to validate subject.rating
which is an integer.
Is it possible to have validations for basic_model (couchdb) in Ruby on Rails?
The validations in ActiveRecord are very coupled with ActiveRecord itself, so you won't be able to easely use AR's validation code outside of AR. They're well aware of this, and Rails 3.0 will have ActiveModel, which decouples it from ActiveRecord, so that you could have done something like this:
class Foo
include ActiveModel::Validations
end
Until then, you could write your own or use the validatable gem.
Related Topics
Rails Activerecord: Saving Nested Models Is Rolled Back
Ruby Devise, Sessionscontroller.Create, JSON - Getting Nameerror: Undefined 'Build_Resource'
Ruby on Rails - Error Making New App (Rb:55:In 'Require')
Rails 3 Translations Within Models in Production
Activerecord::Recordnotfound in Articlescontroller#Show Couldn't Find Article Without an Id
Rails Strong Params - Using Fields from Has_Many Object
Complete Web Apps in Ruby Without Using Any Frameworks - How Difficult? Pitfalls
If Statement in Ruby Using Regex
Rails: Logging the Entire Stack Trace of an Exception
Rails 3 + Daemons Gem: Exception When Querying Model
Check If String Is Repetition of an Unknown Substring
Understanding Usage of Symbols in Routes.Rb Files
Proc Throws Error When Used with Do End
Accessing Values Through Ajax Calls and Partials in Rails
How to Multiply "5X3" String in Ruby
Ruby: "Unexpected Keyword_End"... But All Openers and Closers Match