I got some old code I'm trying to port over to Rails 5 (reasons)
And I ran into this....
def vol_event_for_date(date)
VolunteerEvent.find_or_create_by_description_and_date("Roster ##{self.id}", date)
end
def vol_event_for_weekday(wday)
VolunteerDefaultEvent.find_or_create_by_description_and_weekday_id("Roster ##{self.id}", wday)
end
I know from a post I've seen on S.O. that the find_by thingie is an old outdated Rails helper of some kind so my question is...how can I refactor this for a Rails 5.0.7 app?
The change is actually very simple as explained in the Rails 4 Active Record Deprecations
find_or_create_by_... can be rewritten using find_or_create_by(...).
VolunteerEvent.find_or_create_by_description_and_date("Roster ##{self.id}", date)
just changes to
VolunteerEvent.find_or_create_by(description: "Roster ##{self.id}", date: date)
Related
I am trying to upgrade my rails 3.0.5 application with ruby 2.3.4. Originally it was ruby 1.9.3. I was able to fix most things by updating the gems. However, i m stuck on this one problem where when creating new active record objects, the time does not convert properly.
For example
Product.new(:bought_on => Date.today) will save the object with bought_on to be the date, not datetime.
I was able to narrow down the problem to the file
activerecord-3.0.20/lib/active_record/attribute_methods/time_zone_conversion.rb
For some reason its not calling these two functions, define_method_attribute and define_method_attribute=.
Any ideas?
I found the issue, the define_method_attribute under time_zone_conversion.rb is a protected method, and in ruby 2, the respond_to function always returns false for protected methods. Had to monkey patch to remove the protected attribute.
I am upgrading from Rails 3.2 to 4.2 and wanted to follow Ryan Bates' advice of getting things working as quickly as possible before doing any major refactoring.
To that end, I installed the protected_attributes gem because I was under the impression that with this gem installed I wouldn't need to implement the strong params approach in my controllers immediately and could continue using attr_accessible in the models until I have time to refactor.
I'm not getting any errors about attr_accessible itself, but when I try to create a user in development I get Unpermitted parameters: first_name, last_name, phone despite having all of those as arguments in the User model's attr_accessible method.
Can someone point out what I'm doing wrong here?
That's not the correct approach. Instead of porting a legacy, deprecated feature from 3.2 to 4.2, what you really want to do instead is the opposite: install strong_parameters gem in Rails 3.2 and make sure to replace the attr_accessible before the upgrade.
Rails 4.x is not really designed to use protected attributes anymore, therefore you will encounter a lot of issues trying to reintroduce it.
To use strong params you will have to update your controller's code (which is what I recommend to do, since it won't cost too much work).
In general the implementation of using strong_parameters is as follows:
def create
#model = Model.create(model_params)
if #model.persisted?
# logic
else
#logic
end
end
private
def model_params
params.require(:model).permit(:model_attrbite1, :model_attribute2)
end
I need to run a method once daily in a Rails application.
I'm thinking about using the whenever gem, but I realize I have a bit of a problem.
I need to run the method on an instance of an object. So I can't just have something like:
every 1.day do
runner 'Object.method'
end
I'm thinking about doing something like
every 1.day do
runner 'Object.where(status: "the_status_i_want").each { |object| object.method }'
end
But I have no clue how to test this behavior from within 'whenever.'
Any ideas?
Ryan Bates has a nice episode on integrating whenever gem into Rails
http://railscasts.com/episodes/164-cron-in-ruby-revised
(There is also a free version on that site, but it's kinda old, http://railscasts.com/episodes/164-cron-in-ruby)
According to Rails engines extending functionality in Rails 2.x one could do
Rails::Initializer.new(Rails.configuration).plugin_loader.engines
This code is not working in Rails 3
ActionController::RoutingError (undefined method `new' for Rails::Initializer:Module):
config/application.rb:12:in `require_or_load'
What do I need to do in Rails 3 to get such list of engines?
This is needed for Extending controllers of a Rails 3 Engine in the main app
This has changed with Rails 4.1. The accepted answer is deprecated and the new way to get the installed Engines for a Rails application is now:
::Rails::Engine.subclasses.map(&:instance)
Here's a reference to the commit in github making the change (and also showing how it was implemented after initial deprecation...)
If you need to use the previous solution from Rails 4.1:
module Rails
class Engine
class Railties
def self.engines
#engines ||= Rails::Engine.subclasses.map(&:instance)
end
end
end
end
As of 5/10/2011 and Rails 3.1 beta, it's now
Rails::Application::Railties.engines
Try:
Rails::Application.railties.engines
In Rail 2.3.2 can I have merb style action definitions:
Eg: instead of
def show
#user = User.find(params[:id])
end
Can I have:
def show(id)
#user = User.find(id)
end
What kind of crazy monkey patching do I need to do to get this working, note I only need this working for MRI so ParseTree is an option.
Note: there is a Rails 3 port of this functionality now.
You could change all params in to instance var's (eg. #id) in a before_filter...
EDIT: I was wrong: Method#parameters has been added to Ruby 1.9.2.
Original:
No, this is not possible in Rails.
There is almost zero chance of this making it into Rails 3.0. merb-action-args used ParseTree which doesn't and will not work on Ruby 1.9, therefore making it unlikely that it will be included in Rails.
I am running Rails 2.3.3 and User.find(id) works just fine.