What is the substitue for the attr_protected in rails 5? - ruby-on-rails-5

class Oregano < ApplicationRecord
attr_protected :person_id
end
This throws me back with an error
/activerecord-5.0.5/lib/active_record/dynamic_matchers.rb:21:in `method_missing': undefined method `attr_protected' for #<Class:0x000055b38448bd58> (NoMethodError).
What should be the substitue in the rails 5 upgrade for this?

No mass assignment allowed for Rails 5
Instead of having attr_protected :person_id in your model, use strong parameters. You'll do this in your OreganosController:
def oregano_params
params.require(:oregano).permit(:person_id)
end

Related

Before filter in action mailer Rails 3

What I need:
Something similar to before_filter in ActionMailer in Rails 3.
Problem:
I am working on Rails 3 and want to have a before_filter in ActionMailer. Checked the actionmailer api and learned about before_action and after_action callbacks. When implemented it gives the error:
NoMethodError: undefined method `before_action' for Notifier:Class
Later learned that there is no before action call for Rails 3 from this post
Isnt there any hook or gem so that we can have something similar like before_filter in Rails 3.
Please help. Many Thanks!!
It can be achieved by including AbstractController::Callbacks. This mimics the change to Rails 4 which apart from comments and tests, just included Callbacks.
class MyMailer < ActionMailer::Base
include AbstractController::Callbacks
after_filter :check_email
def some_mail_action(user)
#user = user
...
end
private
def check_email
if #user.email.nil?
message.perform_deliveries = false
end
true
end
end
Reference - How to add a before_filter in UserMailer which checks if it is OK to mail a user?

NoMethodError when using :include on association in Rails 3 with Active Record

Obviously I'm missing something simple here. Here are my two classes and the code I'm calling. When I use :include with find, it blows up and gives me a NoMethodError on the find line. When I don't use :include it works just fine (but obviously doesn't do the join)
Called Code
def index
#users = User.find(:all, :include => [:org])
end
Classes
class User < ActiveRecord::Base
belongs_to :org, :primary_key => :org_id
end
class Org < ActiveRecord::Base
#define primary key because it is not :id
#because this table is in an old db
#that can't be changed
set_primary_key :org_id
has_one :user
def full_name
"#{emp_fname} #{emp_lname}"
end
end
The exact error
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each):
What version of rails are you on? Your tag says 3 - is it 3.1 or one of the 3.0.x series? Regardless, it seems in 3.0.x this :include hash syntax for find isn't supported.
Try User.includes(:org).find(:all).
Ok, so after a lot of digging and trial and error, basically it was a combination of having one table in one schema and the other in a different schema. By specifying the fully qualified table name for our active record objects, active record stopped throwing up on itself.
So the final code:
Called Code
def index
#users = User.includes(:org)
end
Classes
class User < ActiveRecord::Base
set_table_name "DOC_REQUEST.USERS"
belongs_to :org, :primary_key => :org_id
end
class Org < ActiveRecord::Base
set_table_name "AOS.ORG"
#define primary key because it is not :id
#because this table is in an old db
#that can't be changed
set_primary_key :org_id
has_one :user
def full_name
"#{emp_fname} #{emp_lname}"
end
end

Rails 3 - uninitialized constant Setting::Paymentshop

I am getting still this error, Setting and Paymentshop are models.
class Setting < ActiveRecord::Base
has_many :paymentshops
end
class PaymentShop < ActiveRecord::Base
belongs_to :setting
end
In view I have problem on this line:
dopr.paymentshops.type_v
dopr is variable with data from Setting and type_v is column in table Paymentshops.
I would like to ask you, If could anyone help me please with this error...
Thanks
Rails tries to automatically infer the model name from the relation name. With no indication of where to break the single lower-case stream of characters, it assumes that the target model is called Paymentshops.
You can explicitly override the expected class name with has_many :paymentshops, :class_name => "PaymentShop". Alternatively, you could try using has_many :payment_shops - I'm not 100% sure how Rails modifies the relation names, but I think that should map to PaymentShop directly.

How to add a Hash object to an ActiveRecord class? Tried but migration fails

I want my ActiveRecord class User to contain options (a bunch of string key-values), so I wrote:
rails generate migration AddOptionsToUser options:Hash
It generated:
class AddOptionsToUser < ActiveRecord::Migration
def self.up
add_column :users, :options, :Hash
end
def self.down
remove_column :users, :options
end
end
I also added this line to my class User:
serialize :options, Hash
But the migration fails:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Hash' at line 1: ALTER TABLE `users` ADD `options` Hash
I am new to Rails, what is the usual way to store a bunch of string key-values in an ActiveRecord class?
Rails serializes things in to a (YAML) string. So in your database, the type should be string (or text).
class AddOptionsToUser < ActiveRecord::Migration
def self.up
add_column :assessments, :options, :string
end
def self.down
remove_column :assessments, :options
end
end
To have ruby object as an attribute of the ActiveRecord model you should use serialize method inside your class for that attribute link

Model Not Recognized after Upgrading to Rails 3

I have just upgraded the application from Rails 2 to Rails 3 (ruby 1.9.2-head).
Most models are recognized after the upgrade, however one model called "Villa" is not recognized at all and returns an "undefined method" error if you try to call any methods on it (e.g Villa.find(1) ).
The simplified code for the model is, but I have changed this in every which way and it doesn't seem to solve the problem:
class Villa < ActiveRecord::Base
belongs_to :beach
has_many :villa_pictures, :order => "id ASC"
has_many :villa_rooms, :order => "id ASC"
has_many :villa_facilities
default_scope :conditions => ["active = ?", "true"]
end
From the console, typing in "Villa" will simply return => Villa, whereas the other models will return their table definition.
Thanks in advance.
The Villa constant might already be defined. You don't have any other classes or modules elsewhere? Or is your application called "villa"? That would define Villa in config/application.rb by default.