I would like to skip/disable ActiveRecord callbacks in, specifically, Rails 3. The following is an example solution I thought of -- creating an attribute that is defined to create the object without callbacks.
product = Product.new(title: 'Smth')
product.send(:create_without_callbacks)
The above example is similar to that in this answer, but the author said it is for specifically Rails 2. Is there a similar, or even better, way to do this for Rails 3?
See the question: How can I avoid running ActiveRecord callbacks?
This blog post has another explanation with example.
You may find the solution here helpful: http://manuelvanrijn.nl/blog/2012/01/12/disable-rails-before-slash-after-callback/
TL;DR - If you are doing this production, it may be helpful to create a class (see blog post) to keep your code DRY. Otherwise, if you do this once, or even in testing (like myself), you can simply do the following:
SomeModel.skip_callback(:save, :before, :before_action)
SomeModel.skip_callback(:save, :after, :after_action)
The blog post provides a nice list of callbacks the above method will work with.
This will work in both Rails 3 and Rails 4. As noted by a comment, if you have to disable callbacks, you may want to ask yourself why you need those callback. To expand, disabling callbacks are questionable only in production. If you need to disable them in testing (which is what I am doing myself), it is acceptable... Especially since Rails 4 core deprecated the use of observers.
SomeModel.skip_callback(:save) do
somemodel_instance.save
end
tested: Rails 4.2.1
Related
Sorry this is probably a question that has been asked many times but as it regards what is a very common word in English it is pretty much impossible to google or search for it.
I have seen a few examples of Ruby code which looks like this:
EnquiryNotification.with(post: #post)
I'm trying to understand what the ".with" part does. Can anyone shed some light on this please?
with is a class method defined on the EnquiryNotification class (or one of its ancestors). The method is neither a keyword in the Ruby language nor is it a common method on classes shipped with Ruby itself or its standard library. As such, to find what this method does, you would likely have to consult the documentation of your chosen framework or application.
An example of a with method defined in a framework is Sequel::Dataset#with to add a CTE to the current query. The method is also available as a class method in Sequel model classes.
It could also be part of ActionMailer as mentioned by Stefan in a comment above.
In any case though, make sure to consult the documentation of your chosen framework or library for details.
I have updated active record from 3.2.12 to 4.1.4. Accordingly I have update activesupport also from 3.2.12 to 4.1.4. Now when I do data base queries (h2-data base) I cannot use
mrs = MeasurementResultSample.find_by_duration_and_measurement_result_id(duration, id)
I can oly use
mrs = MeasurementResultSample.where(:duration => duration, :measurement_result_id => id).first
The activerecord-jdbch2-adapter I have upgraded from 1.2.9 to 1.3.9
How can I make my 'find_by' methods using one or more fields working again?
Update:
actually it turned out, that find_by is working but I had to check if the table exsist with the method exists?. What wont work is find_last_by... or find_all_by... or find_first_by... .
As I understand it, Rails is moving away from certain 'magic' method names due to the poor performance of 'method_missing,' upon which they're based.
However, find_by works. So you can re-work your query with:
mrs = MeasurementResultSample.find_by(duration: duration, measurement_result_id:, id)
Amusingly, the implementation behind the scenes is:
def find_by(*args)
where(*args).take
end
http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
Edit:
There's a gem that includes Rails 4 deprecated finders:
https://github.com/rails/activerecord-deprecated_finders
Confusingly it states that find_by... methods aren't deprecated. However, if you've updated ActiveRecord outside Rails it might help.
If not, you could always look at the Rails 3 ActiveRecord code and port the relevant methods across.
Thanks to A Fader Darkly! I finally started to port the code to activerecord 4.1.4. I opted to change the finder methods to where(...).
Here I found some information too what-s-new-in-active-record-rails-4.
It's the cleanest way. I noticed, that I had to use the exist? method in some cases to make sure the table is not nil. I did not have to do this before (in 3.2.12) with inflections used.
Question:
In a post-attr_accessible Rails 4 world, in what way do you recommend, if at all, annotating your ActiveRecord model class files to communicate its (database) attributes?
Further Thoughts
As part of a Rails 3 -> 4 upgrade, we are making a switch, and happily so, away from attr_accessible and to strong parameters in the controller. I understand and agree with the improvement in security via this switch. If you want to know more about this, the information is out there, and it's not hard to find.
But, I enjoyed, in my Rails 3 world, having those reminders of what attributes made up a class up there at the top of the model file. Especially since we're moving toward a world in which ActiveRecord classes are just DAOs, what else is the class but a collection of database attributes? I don't want to go to the schema.rb file just to remember them.
Am I thinking about this incorrectly? In a DAO world, should I be creating my ActiveRecord model class file and then never opening it again?
I know about the annotate_models gem and used it way back in the day. I was not a fan of having the attributes described in commented-out lines. (unreadable, hackish, fragile)
Thoughts? Opinions?
How about:
Person.column_names
If you are using an IDE or and editor that has a console feature this becomes an easy way to be reminded what attributes there are. I am no Ruby or Rails expert, still pretty new here, but I've been using Rails 4 almost exclusively and it just seems like you wouldn't need to see the attributes that often in the model. The params get whitelisted in the controller because that is where they will usually be used, no? If you don't want to use comments you could store an array of the attributes in the model:
my_attr = [:fname, :lname, :age, :height, :weight]
But is that really any more useful than a comment? Would there be a case of attributes that would have been in attr_accessible that wouldn't be in your whitelist in your controller? It would be trick if you put some code in a rake task that would run every time you ran
rake db:...
that would update the my_attr array in your model so you wouldn't have to remember to do it when you modified the model. I go into my models to add class methods and scopes, so I do see a value in it. But I work in RubyMine so I just click on the DB tab on the left side if I need to be reminded of columns that aren't in my whitelist.
I'm doing a lot of bulk data-validation on various kinds of data sources and I find myself writing such boilerplate code:
if summed_payments != row['Total']
raise "The sum of the payments, #{summed_payments} != #{row['Total']}"
end
I was wondering if there was a way to apply a DSL, like Minitest, but to use it for purposes that didn't involve application testing? For example, finding and logging errors during a bulk data import and validation script...it's a quick-and-dirty script that I don't want to have to write a test suite for, but that I want to do some various kinds of validation upon.
I think standalone ActiveModel should be good for this.
Watch this railscast for more information: http://railscasts.com/episodes/219-active-model
If you like you can check out http://rubygems.org/gems/validates_simple which is a gem for doing simple validation of hashes trying to mimic the interface of the active model validation methods.
I know this is a little old, but our Veto gem is likely what you're looking for: https://github.com/kodio/veto
Standalone validation of plain old ruby object, without dependencies.
I've recently 'discovered' Php-activerecord for use with CodeIgniter (using Sparks).
It is surely an easier way of building queries and getting data in and out of the database
than coding the models for yourself.
So my question is: is somebody using php-activerecord, instead of CodeIgniter's builtin activerecord, for a large project (ie. larger than the typical, tutorialish 'blog' example)?
What pitfals are there, when moving on from CodeIgniter models?
Thanks
ActiveRecord seems to be a well known pattern. In your particular case you should bare in mind, that you no longer need the CodeIgniter AR classes and methods.
Extending the php-activerecord models gives you many standarized methods dealing with your data. That's a good portion of code you would have to type in for yourself over and over again in each project (and beacause of the common CRUD actions, the code would be almost the same).
Great library!
I use it with my current CMS under development.
Once you get your head around relationships its a breeze. Activerecord\Model class is a joy to work with I find, however it would be nice to have a benchmarking wrapper. Really though if you have set your database architecture up right by indexing your foreign keys it should help.
Some nice features
callbacks
eager-loading
I have not personally used php-activerecord but it looks like a good ORM with good documentation. It looks like a nice time saver for larger projects, but for smaller ones i'd probably just stick with the standard activerecord class.