I need execute the method name before the record is saved in the database, but it seems not working
class Guest < ActiveRecord::Base
before_save :name
has_and_belongs_to_many :checkins
validates_presence_of :FirstName
validates_presence_of :LastName
validates_format_of :FirstName, with: /^[A-Za-z\s]+$/
validates_format_of :LastName, with: /^[A-Za-z\s]+$/
def name
self.FirstName.titleize + " " + self.LastName.titleize
end
end
console
=> Guest(id: integer, FirstName: string, LastName: string, age: integer, sex: string, photo: string, address: text, mobile: integer, email: string, birthdate: date, created_at: datetime, updated_at: datetime)
1.9.3-p547 :008 > f=Guest.new
=> #<Guest id: nil, FirstName: nil, LastName: nil, age: nil, sex: nil, photo: nil, address: nil, mobile: nil, email: nil, birthdate: nil, created_at: nil, updated_at: nil>
1.9.3-p547 :009 > f.FirstName=" fernando "
=> " fernando "
1.9.3-p547 :010 > f.LastName=" suarez"
=> " suarez"
1.9.3-p547 :011 > f.save
=> true
1.9.3-p547 :012 > Guest.last
=> #<Guest id: 9, FirstName: " fernando ", LastName: " suarez", age: nil, sex: nil, photo: nil, address: nil, mobile: nil, email: nil, birthdate: nil, created_at: "2015-04-26 00:16:38", updated_at: "2015-04-26 00:16:38">
You need to change
self.FirstName.titleize + " " + self.LastName.titleize
to
self.FirstName = self.FirstName.titleize
self.LastName = self.LastName.titleize
Explanation: You didn't actually change anything, but concatenate the values.
But probably you want to use the name method for your name-representation. So I'd suggest a new method for the validation.
It would look like this:
def titlelize_names
self.FirstName = self.FirstName.titleize
self.LastName = self.LastName.titleize
end
And the before_save must call :titlelize_names, of course.
Related
I've been getting intermittent errors while seeding with rails. I'm hoping someone can help provide some insight into the different types of the User class.
The error in full:
ActiveRecord::AssociationTypeMismatch: User(#35560) expected, got #<User id: "bedc7c4e-cdd2-4ea1-a7ee-4e6642467fba", email: "phil#email.domain", jti: "7376cf41-7f88-407d-8365-1e311d946b88", ios_device_token: nil, fcm_device_token: nil, first_name: "Phil", last_name: "6", phone_number: nil, date_of_birth: nil, super_user: true, created_at: "2023-02-08 08:16:37.559974000 +0000", updated_at: "2023-02-08 08:16:37.559974000 +0000"> which is an instance of User(#22700)
The code which causes it:
user = User.new(
first_name: 'Phil',
last_name: '6',
email: 'phil#email.domain',
super_user: true,
password: 'test1234'
)
user.skip_confirmation!
user.save!
organisation = Organisation.find_by_name('Team')
Membership.create!(
user:,
organisation:,
verified: true,
verified_at: now,
organisation_admin: true,
shift_admin: true,
email: 'phil.6#group.com',
email_confirmed: true,
category: organisation.categories.find_by_name('Developer')
)
organisation = Organisation.find_by_name('Test Org')
membership = Membership.create!(
user:,
organisation:,
verified: true,
verified_at: now,
email: 'phil#testorg.com',
email_confirmed: true
)
If I pause execution before the error I can see that user == User.first is false despite User.first and user being these two lines, which are visually identical:
#<User id: "6ce62b08-cf4c-4bfa-878a-02a1ed889c69", email: "phil#email.domain", jti: "710948b6-5f4f-40ea-ab9f-df8e3b1219c3", ios_device_token: nil, fcm_device_token: nil, first_name: "Phil", last_name: "6", phone_number: nil, date_of_birth: nil, super_user: true, created_at: "2023-02-08 08:17:06.024800000 +0000", updated_at: "2023-02-08 08:17:06.024800000 +0000">
#<User id: "6ce62b08-cf4c-4bfa-878a-02a1ed889c69", email: "phil#email.domain", jti: "710948b6-5f4f-40ea-ab9f-df8e3b1219c3", ios_device_token: nil, fcm_device_token: nil, first_name: "Phil", last_name: "6", phone_number: nil, date_of_birth: nil, super_user: true, created_at: "2023-02-08 08:17:06.024800000 +0000", updated_at: "2023-02-08 08:17:06.024800000 +0000">
It's the same thing if I compare user.class and User.first.class, they look the same but a comparison evaluates to false.
Am I doing something to mutate the local variable?
What you should be doing here is to create an assocation:
class User < ApplicationRecord
has_many :memberships
end
Then you create the memberships through that assocation instead:
user = User.create!(
first_name: 'Phil',
last_name: '6',
email: 'phil#email.domain',
super_user: true,
password: 'test1234',
confirmed_at: Time.current # the easy way to skip Devise::Confirmable
)
# make sure you use the bang method so that you're not just getting a nil
organisation = Organisation.find_by_name!('Test Org')
user.memberships.create!(
organisation: organisation,
verified: true,
verified_at: now,
organisation_admin: true,
shift_admin: true,
email: 'phil.6#group.com',
email_confirmed: true,
category: organisation.categories.find_by_name!('Developer')
)
I was setting up Devise & Omniauth as per railscasts like I usually do, only now I'm using Rails 5.0.1 and ruby 2.3.3
When I go to add a user, not even using Omniauth, just using a basic email + password + confirmation the page reloads with an error of "Email can't be blank". I can see the parameters include the email.
I used Pry to investigate - things get weird here.
[1] pry(#<RegistrationsController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"[removed this!]", "user"=><ActionController::Parameters {"email"=>"test#test.com", "password"=>"testtest", "password_confirmation"=>"testtest"} permitted: false>, "commit"=>"Sign up", "controller"=>"registrations", "action"=>"create"} permitted: false>
[3] pry(#<RegistrationsController>)> #user
=> #<User id: nil, email: "test#test.com", created_at: nil, updated_at: nil, first_name: nil, last_name: nil, image_url: nil, headline: nil, dob: nil, gender: nil, webpage_url: nil, twitter: nil, linkedin: nil, blog_url: nil, description: nil, country: nil, state: nil, city: nil, phone: nil, full_bio: nil, profile_claimed: nil>
[4] pry(#<RegistrationsController>)> #user.class
=> User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, first_name: string, last_name: string, image_url: string, headline: string, dob: date, gender: string, webpage_url: string, twitter: string, linkedin: string, blog_url: string, description: text, country: string, state: string, city: string, phone: string, full_bio: text, type: string, profile_claimed: boolean)
[5] pry(#<RegistrationsController>)> #user.email
=> nil
[7] pry(#<RegistrationsController>)> #user.errors
=> #<ActiveModel::Errors:0x007f9de2c82da0
#base=
#<User id: nil, email: "test#test.com", created_at: nil, updated_at: nil, first_name: nil, last_name: nil, image_url: nil, headline: nil, dob: nil, gender: nil, webpage_url: nil, twitter: nil, linkedin: nil, blog_url: nil, description: nil, country: nil, state: nil, city: nil, phone: nil, full_bio: nil, profile_claimed: nil>,
#details={:email=>[{:error=>:blank}, {:error=>:blank}]},
#messages={:email=>["can't be blank"], :password=>[], :password_confirmation=>[]}>
[8] pry(#<RegistrationsController>)> #user.email.class
=> NilClass
You can see that if I access #user it returns the hash containing the email. However if I try #user.email I get nil. And when I ask for #user.errors the message says email can't be blank.
I've been hunting a while on this one. Thanks in advance for any help!
--
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:email])
end
end
Puma server.
Started POST "/users" for ::1 at 2017-01-07 20:42:57 -0500
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"tfBeZtXEFdZV9Rx3dJgCQJ+9hHuvgFcbc3dD9D7Q6yKki4GmlVw17/qlDm2squ3hKngX3Juu12iaM1Dki9qXig==", "user"=>{"email"=>"test#test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendering registrations/new.html.erb within layouts/application
Rendered devise/shared/_links.html.erb (0.7ms)
Rendered registrations/new.html.erb within layouts/application (4.6ms)
--
rails console
Running via Spring preloader in process 12578
Loading development environment (Rails 5.0.1)
2.3.3 :001 > u = User.new
=> #<User id: nil, email: "", created_at: nil, updated_at: nil, first_name: nil, last_name: nil, image_url: nil, headline: nil, dob: nil, gender: nil, webpage_url: nil, twitter: nil, linkedin: nil, blog_url: nil, description: nil, country: nil, state: nil, city: nil, phone: nil, full_bio: nil, profile_claimed: nil>
2.3.3 :002 > u.email = "test#test.com"
=> "test#test.com"
2.3.3 :003 > u.password = "password"
=> "password"
2.3.3 :004 > u.password_confirmation = "password"
=> "password"
2.3.3 :005 > u.save
(0.2ms) begin transaction
(0.1ms) rollback transaction
=> false
2.3.3 :006 > u.errors
=> #<ActiveModel::Errors:0x007f87163eeac8 #base=#<User id: nil, email: "test#test.com", created_at: nil, updated_at: nil, first_name: nil, last_name: nil, image_url: nil, headline: nil, dob: nil, gender: nil, webpage_url: nil, twitter: nil, linkedin: nil, blog_url: nil, description: nil, country: nil, state: nil, city: nil, phone: nil, full_bio: nil, profile_claimed: nil>, #messages={:email=>["can't be blank"]}, #details={:email=>[{:error=>:blank}, {:error=>:blank}]}>
I believe permitted: false says that your parameters are not permitted and can't be saved. Did you set up strong parameters for Users controller?
I would like to edit a supplier name in my heroku database. I'm having trouble accessing the name attribute:
irb(main):015:0> Supplier.where(:name => "Test")
=> #<ActiveRecord::Relation [#<Supplier id: 3070, name: "Test", email: "test#me.com", phone: "555555", website: "http://www.test.co.uk", region_id: 3, category_id: 8, created_at: "2015-02-20 13:28:59", updated_at: "2015-02-20 13:28:59", rating: 0.0, address: nil, facebook_url: nil, twitter_url: nil, google_url: nil, video_url: nil, slug: "test", logo_url: nil, image_one_url: nil, image_two_url: nil, image_three_url: nil, image_four_url: nil, description: nil, reviews_count: 0, source: nil, source_other: nil>]>
irb(main):016:0> _.name
=> "Supplier"
I'm not clear why _.name is resulting in "Supplier" rather than "Test".
Can anyone tell me what I'm missing?
Supplier.where(:name => "Test") returns multiple records. Use
supplier = Supplier.where(:name => "Test").first
supplier.name
When I comment out my after_save call back, my ActiveRecord associations work just fine. In Rails Console, you'd see:
> #report = Report.create :name => "foo"
=> #<Report id: 9, name: "foo", created_at: "2013-03-05 09:51:55", updated_at: "2013-03-05 09:51:55">
> #question = #report.questions.create :description => "bar"
=> #<Question id: 18, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 09:52:32", updated_at: "2013-03-05 09:52:32", additive: false, instructions: nil>
> #report.questions
=> [#<Question id: 18, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 09:52:32", updated_at: "2013-03-05 09:52:32", additive: false, instructions: nil>]
> #question.reports
=> [#<Report id: 9, name: "foo", created_at: "2013-03-05 09:51:55", updated_at: "2013-03-05 09:51:55">]
However, the associations stop working when I add the following after_save callback to question.rb:
def create_matching_surveys
self.reports.each do |report|
report.reviews.each do |review|
review.competitors.each do |competitor|
competitor.surveys.find_or_create_by_question_id(self.id)
end
end
end
end
Then, in Rails Console, you get:
> #report = Report.create :name => "foo"
=> #<Report id: 13, name: "foo", created_at: "2013-03-05 10:20:51", updated_at: "2013-03-05 10:20:51">
> #question = #report.questions.create :description => "bar"
=> #<Question id: 24, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 10:21:02", updated_at: "2013-03-05 10:21:02", additive: false, instructions: nil>
> #report.questions
=> [#<Question id: 24, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 10:21:02", updated_at: "2013-03-05 10:21:02", additive: false, instructions: nil>]
> #question.reports
=> []
This happens whether or not the report has reviews that have competitors.
The strange thing is I thought the callback was meant to happen after the question was saved? So by rights the association should save too before any of this happens, right?
How do I fix it?
UPDATE
I think I have to call the callback in the right spot in the object's life cycle, but I can't find that spot. Here's why I think this:
> #report = Report.create :name => "foo"
=> #<Report id: 20, name: "foo", created_at: "2013-03-05 12:29:35", updated_at: "2013-03-05 12:29:35">
> #question = #report.questions.create :description => "bar"
=> #<Question id: 31, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 12:30:14", updated_at: "2013-03-05 12:30:14", additive: false, instructions: nil>
> #question.reports
=> []
> #question.update_attributes :description => "foo"
=> true
> #question.reports
=> [#<Report id: 20, name: "foo", created_at: "2013-03-05 12:29:35", updated_at: "2013-03-05 12:29:35">]
BTW, the method is now in question_observer.rb:
class QuestionObserver < ActiveRecord::Observer
def after_save(model)
model.reload
model.reports.reload
model.reports.each do |report|
report.reviews.each do |review|
review.competitors.each do |competitor|
competitor.surveys.find_or_create_by_question_id(model.id)
end
end
end
return true
end
end
The answer was to use a neat new callback hook called after_commit which was introduced with Rails 3.
See http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_commit.
The only issue is after_commit doesn't work "out of the box" with transactional fixtures, but there are plenty of solutions out there, and I found this one worked well for me: https://supportbee.com/devblog/2012/01/14/testing-after_commitafter_transaction-with-rspec/
I have a pair of classes:
class Collection < ActiveRecord::Base
has_many :items, autosave: true
end
class Item < ActiveRecord::Base
belongs_to :collection
end
From the docs:
When :autosave is true all children is saved, no matter whether they are new records:
But when I update an Item and save its parent Collection, the Item's upated attributes don't get saved:
> c = Collection.first
=> #<Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10">
> i = c.items.first
=> #<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">
> i.name = 'new name'
=> "new name"
> c.save
=> true
> Collection.first.items
=> [#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]
So, what am I missing?
I'm using Rails 3.2.5 and Ruby 1.9.2.
So I've done some digging about in the source of ActiveRecord. We can get hold of c's autosave assocations:
> c.class.reflect_on_all_autosave_associations
=> [#<ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 #macro=:has_many, #name=:items, #options={:autosave=>true, :extend=>[]}, #active_record=Collection(id: integer, name: string, created_at: datetime, updated_at: datetime), #plural_name="items", #collection=true, #class_name="Item", #klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), #foreign_key="collection_id", #active_record_primary_key="id", #type=nil>]
I think this illustrates that the association has been set up for autosaving.
We can then get the instance of the association corresponding to c:
> a = c.send :association_instance_get, :items
=> #<ActiveRecord::Associations::HasManyAssociation:0x007fece738e920 #target=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #reflection=#<ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 #macro=:has_many, #name=:items, #options={:autosave=>true, :extend=>[]}, #active_record=Collection(id: integer, name: string, created_at: datetime, updated_at: datetime), #plural_name="items", #collection=true, #class_name="Item", #klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), #foreign_key="collection_id", #active_record_primary_key="id", #type=nil>, #owner=#<Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10">, #updated=false, #loaded=true, #association_scope=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #proxy=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #stale_state=nil>
We can then find the actual objects that are associated via this association:
> a.target
=> [#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]
The object found here does not have update that I'd made earlier.
The problem here is the line
i = c.items.first
This line pulls the correct item from the database, but doesn't attach it to the collection c. It is a distinct ruby object from the object
i = c.items[0]
If you replace the first line with the second your example will work.