search for: before_create

Displaying 20 results from an estimated 107 matches for "before_create".

2012 May 26
2
before_create is after_validatation :on => :create , when it should be before_validation :on => :create
In ActiveRecord''s callbacks, methods within a before_create callback are called after the validation, but before the model is created. This was an intentional design decision, but I think it was the wrong one. What do you think? When I think of before_create, I think of before_validation :foo, :on => :create, but, by default, before_create is synonymou...
2006 Feb 15
1
self in before_create
Hi, I am having a small problem concernig how to access local variables in before_create. I am not sure how to really form this question but here goes: I am wondering why this works def before_create self.last_logged_in = Time.now self.created = Time.now end but this doesn''t def before_create @last_logged_in = Time.now @created = Time.now end The l...
2006 Apr 21
2
Using before_create and conflicting setter method...
Hi, I am having trouble using before_create when I have an specialized setter method: before_save :set_campaign_start #----------------------------------------------------------------------- ------- def set_campaign_start self.campaign_start = Time.now end # We want the date to always start at midnight #-...
2007 May 05
3
DB trigger vs before_create
...g trouble figuring out which approach is better. I have the following tables: products, orders and line_items. Every time an Order is placed, the quantity field in the products table has to be updated for each LineItem in the Order. One option is to use DB triggers which is not as portable as using before_create in the Order model. But with before_create I cannot use fixtures for orders and line_items since they don''t use before_create as models do. Has anyone any recommendations on which approach is better? Erik --~--~---------~--~----~------------~-------~--~----~ You received this message b...
2007 Jan 08
2
before_create firing twice on a save - RC2
I have a unit test which fails using Rails 1.2RC2, because a method marked by the before_create filter is firing twice when my ActiveRecord is saved. Is a before_create filter supposed to cause multiple invocations? We have assumed it is called once, and only once, when the object is created, in our code (but we unit tested it to make certain). This is one of three issues I have with my uni...
2005 Dec 30
2
before_create question
...r, Registration, Event. User has_many Registration, Event has_many Registration, and Registration belongs_to User and Registration. So far, so good. There''s a registration deadline on a Event though, a few days before the Event occurs. So, I have this: class Registration < AR def before_create errors.add_to_base("Registration deadline has passed") unless event.can_still_register? end end The weird thing is, if the registration deadline has passed, the error gets added to the registration object, and calling save! throws no exception, but the registration object doesn'...
2006 Sep 09
2
why doesn't this work (setting attribute in "before_create")
Hi, Can anyone spot why the below doesn''t work? * I have a "created_by" column in my create table in the dB * I have a "before_create" line, which definitely gets called * I tried a few different ways to set the "created_by" attribute in the model but it doesn''t seem to work, i.e. doesn''t appear in the database ============================= class Contact < ActiveRecord::Base before_create :ge...
2008 Jun 29
3
Working around/with Restful Authentication
I''m using Restful Authentication, and the code to create a user is pretty straight forward - there is a before_save action and a before_create action: before_save :encrypt_password before_create :make_activation_code But for some reason when I try to create a user programmatically in the controller like this: User.new(:email => ''user-n8tE+Mx9DRM@public.gmane.org'', :password => ''123456'', :pa...
2006 Feb 15
8
Agile book - getting confusing error
...eginning phase from the Agile book on ''Administration'' undefined method `hashed_password='' for #<User:0xb7911324> ... /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1498:in `method_missing'' #{RAILS_ROOT}/app/models/user.rb:12:in `before_create'' ./script/../config/../app/controllers/login_controller.rb:8:in `add_user'' ... Request Parameters: {"user"=>{"name"=>"Craig White", "login"=>"craig", "password"=>"test"}} OK... My user.rb includes....
2008 Feb 09
0
a problem with before_create and validations
I stumble across some strange problem. I have a model with a virtual attribute file_contents that is a string. The model has validates_format_of :file_contents, :on => create, :unless => no_contents? (The method that returns file_contents.nil?) Then, I have a before_create callback, where I get my model''s title and description by parsing file_contents, and do write_attribute for title and description. The problem is, that it hangs the server. Forever. Looks like write_attribute triggers validation again, and then before_create is called again, and we get an...
2009 Jun 11
8
before_create return value breaking object.save: rails bug?
I know that usually when people say ''i think i found a bug in rails/ruby'' they''ve done something dumb. I''m wondering if this is the case here, but this does seem like a genuine rails bug to me. In my user model i have a few different before_create callbacks. In one of them, if it happens to return false (just because the last statement in it evaluated to false), then the saving gets blocked - i can see the db transaction rolling back. However, this doesn''t affect the results of calling .valid? on the object, so i get this situatio...
2006 Jul 19
6
ActiveRecord::RecordNotSaved - bizarre behaviour.
...OW FIELDS FROM users SQL (0.000465) BEGIN SQL (0.000466) COMMIT ActiveRecord::RecordNotSaved (ActiveRecord::RecordNotSaved): ... As you can see, there''s no INSERT SQL generated, which is the root cause of the problem. In my user model, I have the following call back: def before_create self.password = Digest::SHA1.hexdigest(self.password) self.rsskey = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) self.otp = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) self.verified = false #logger.debug("--> "+sel...
2006 Apr 03
7
Getters and setters problem?
...odel code: require "digest/sha1" class User < ActiveRecord::Base attr_accessor :password, :login attr_accessible :password, :name, :login, :email validates_confirmation_of :password validates_presence_of :name, :login, :password, :email validates_uniqueness_of :login def before_create self.password=User.hash_password(self.password) self.login="blaat" end def after_create @password=nil end private def self.hash_password(password) Digest::SHA1.hexdigest(password) end end Thanks in advance, Wijnand -- OpenBSD needs your help improving the sof...
2011 Mar 02
6
calbacks on models
Hi, I''m having some strange behavior on callbacks when testing with RSpec2. On my test, when I create a resource using the #create method the callbacks related to #before_create are not called. If I go to the console and I try to type the command Resource.create(client_uri: "http://example.com") all works fine. There are no mocks around, and I can''t understand if this is a normal behavior using RSpec, if its due to Mongoid or something else. # resour...
2011 Jun 27
2
insert value of primary key into another field before record is saved
...I want to also assign it to another field of the same record called ''sequence''. So if the primary key of the record is 23, then the sequence should also be 23. But the record is not created yet, so it doesnt have an id yet. I tried doing this in subbook model: belongs_to :book before_create :add_sequence def add_sequence self.sequence = self.id end But that didn''t do anything at all when running the seed task. Any idea how to update another field with id of record before that record is saved to database? Thanks for response -- You received this message because y...
2006 Jul 25
1
How do I validate using associated objects?
This doesn''t make any sense to me anymore. I have an Upload that belongs_to a Comment. Within my Upload model I expect to be able to access the associated comment with comment.foo. I can only seem to do this AFTER validation. I''m assuming before_create takes place between validation and updating the database, because when I try to access comment.foo within the validate method, I get "You have a nil object when you didn''t expect it! The error occured while evaluating nil.board". Now I am using single table inheritance, but...
2005 Dec 29
7
belongs_to causing NoMethodError exceptions ... ?
...pending_user.used == true # XXX : only for debug. TMI. errors.add_to_base("User request already used") end begin self.account = Account.find(@pending_user.account_id) rescue errors.add(:account_id, "is invalid") end end def before_create self.email_address = @pending_user.email_address self.account = @pending_user.account ##self.pending_user = @pending_user self.pending_user_id = @pending_user.id # XXX: send an email with the activation key here... self.email_validation_key = MiscUtils.random_string(...
2006 Dec 11
1
Compute date during before_validation_on_create
..._date - so, added callback before_validation_on_create - in this method I do : end_date = begin_date >> 12 - unfortunately, beeing in before_validation the actual self.begin_date have not been type casted - It''s always a string - unfortunately again, I can''t put this code in before_create because the validation will not pass because (before_create is called after validation checks) I could remove the validation against end_date but I don''t like this... What will happen if the computed end_date goes wrong ? Thanks for your ideas -- Posted via http://www.ruby-forum.com/....
2012 Nov 10
6
Suggestion: `before_save on: :create` should either work or raise an exception
...a silent failure. I''d like to suggest that it either be made consistent or be made to fail loudly. The issue is that to do something before validating, but only when creating, you use `before_validation on: :create`, but to do something before saving, but only when creating, you use `before_create`. I have tried to use `before_save on: :create`, and it does not produce an error, but it does something unexpected: it saves before create **and** before update. This isn''t really something an application''s tests would catch, I don''t think. I propose one of the fol...
2007 Mar 17
4
Created_on and updated_on in a non-ActiveRecord model
...Well, the thing is that I also want the behaviour of the created_on and updated_on columns, that get modified automatically before saving. The problem is that I have my own "save" method and it does not work (remember this is not a real ActiveRecord model). I''ve tried to set the before_create and before_updated methods but they does not work (see code below). So… is there any way to do it automatically? Thanks. ******* [1] http://wiki.rubyonrails.org/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord , Alternative 2. CODE: class PageMetadata < ActiveRecord::Base def se...