search for: before_sav

Displaying 20 results from an estimated 177 matches for "before_sav".

Did you mean: before_save
2012 Nov 10
6
Suggestion: `before_save on: :create` should either work or raise an exception
...o 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 following changes: 1. `before_save on:...
2011 Oct 14
2
before_save :encrypt_password
I am having a problem finding the best way to make a "before_save :encrypt_password" conditional. I have to at times update user model attributes but each time I do this the password is reencrypted because of the above. I need to differentiate between when the user is first logging in and the password does need to be encrypted, and when they are already l...
2006 Feb 10
4
before_save gotcha
Is it will known and accepted that before_save triggers should return true? I didn''t notice this before but now I see it in the documentation. Here is what I''m doing in my model: def before_save if self.has_album? self.visible = self.album.visible? end end That results in the expected result when album.vi...
2009 Feb 07
5
before_save :strip_whitespace => saves with spaces
i used this private function for removing leading and trailing white spaces from the values.that below function would be called before_save. when i print the value after it strips.it prints string without any spaces.but in the table fields it saves with spaces . waht would be the problem.pls help me class CompanyInfo < ActiveRecord::Base before_save :strip_whitespace def strip_whitespace puts "strip_whitespace"...
2006 Nov 17
4
set empty values as null in the database
Le''s say that I have a table called people and the column middle_name is nullable. If the user enters say blank or empty space then in the database it is recorded as empty space. I would like in all my models all empty spaces to be recorded as null. I guess I can write a plugin which will do so for all the models but I''m sure something like that should already be existing.I
2007 Jun 27
3
acts_as_tree and before_save
We all know that acts_as_tree gives us ability to set up data in a tree-like structure and use methods like ''parent'', ''children'' and so on. Something I''m completely stuck on. Observe. class Page < ActiveRecord::Base acts_as_tree def before_save raise self.parent.to_yaml end end Well, you''d think that it should show element''s parent element but for some reason it''s always blank. Why don''t I have access to parent element here? Parent method seems to work just fine when I actually save the thing....
2006 Jan 03
2
Stopping a save from the before_save
What''s the better thing to do in a before_save if you don''t want the save to continue? raise an exception, or do an errors.add or both? Thanks, Chris Nolan.ca http://kweschun.com/ - Do you have a Kweschun?
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 => ...
2006 Mar 02
2
How to get mime extension with file_column
Is there a way to get the MIME extension of a file_column field? Here''s what I want to do (look for comment in before_save method): class ProductFile < ActiveRecord::Base belongs_to :product file_column :file_name def before_save self.file_type = # something that returns the MIME type (e.g. image/jpeg) end end TIA
2008 Jul 10
6
Uppercase all row columns
Hi. I have to convert the contents of all columns to uppercase before creating a row. Is there an easy way to accomplish this so I don''t have to go one column at a time and upcase! it? Thanks. Pepe --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group,
2007 Dec 26
0
before_save with acts_as_versioned
Yes, I know about the :if/:if_changed clause on acts_as_versioned ... content_changed or author_changed .... That''s not what I''m asking about. What I''m wondering about is something like applying the spiffy new 2.0 sanitize as a ''before_save'' -- A little inaccuracy can save tons of explanation. -- Saki --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/...
2009 Oct 23
0
Sanitize html with :before_save depending on logged in user
I woud like to sanitize attributes (with ''sanitize" gem) but unless the model is being saved by admin. (Suppose this model doesn''t have any relationship with user model) # In model.rb: :before_save set_html_plain def set_html_plain self.body = Sanitize.clean(self.body) end Two questions: 1) How can I check here if saving process has been initialized by specified, logged in user (admin)? 2) What is the best way to abstract these operations to use them easily on multiple models?...
2009 Mar 17
4
Preventing a submitted hash from ActiveRecord DB store
Hi all, I have a multi model form (Project with many tasks) and I want to prevent a task from being saved to the DB if it is empty ie. if there is no i/p for that task from the user. I tried the following class Task < ActiveRecord::Base before_save :check_if_empty ... def check_if_empty self.destroy if description.blank? end but i get this TypeError in ProjectsController#create can''t modify frozen hash Is there a way to let ActiveRecord know not to save a record (which is a frozen hash in this case) to the DB? --~--~--------...
2006 Feb 11
5
after_(read|find) callback?
...9;'s a technique that provides on-the-fly save/read filters. Of course the solution would need to work transparently in joins, so user.posts.last.title would do the right thing if title was an encrypted field. I see in the documentation of ActiveRecord::Callbacks there''s a before_save callback that looks like going in the right direction, but I don''t see the symmetric after_(read|find). Any ideas? -- fxn
2006 Mar 24
2
Access to session data
...sion. I can retrieve and display this information to the input form that is collecting the info, so I know that there is session[:user_name]. Getting it into the active record is more difficult. What I have done is inside the class for member.rb I''ve added a callback method: def before_save self.changed_by = "Rick" end and this will work. However, def before_save self.changed_by = session[:user_name] end does not, complaining that session is undefined :-(. Obviously session is not available everywhere. How do I reference the session correctly...
2006 Aug 09
7
function before_save
Hi everybody I would like a function as the "before_save" method in the model. But it must be the opposite. When I take out data from the database through the model, I want to call a function before the data are available in the controller. Can anyone help me? ---- >>>> thx <<<< ---- -- Posted via http://www.ruby-foru...
2006 Jul 17
5
Keeping attributes of two models in sync
Hi, I have a Product and a corresponding Keyword (1:1 via belongs_to and has_one). I want to ensure, that two attributes (:name) of the two models alway stay in sync. What''s the easiest way to accomplish that? I did play around with before_/after_save hooks and with AR::Observers but in the end I''d always get infinite recursion... Thanks, Timo
2006 Jun 24
0
before_save and before_update not being called in tests
In one of the models I perform some operation. For simplicity let''s assume that I populate column ''b'' with column ''a'' * 2. def before_update self.b = self.a * 2 end In my fixture I set values for a. first: id: 1 a: 10 Whe I run the test before_update is never invoked when rails populate the test table with the data from fixture. In
2006 Apr 08
4
Calling validates_inclusion_of out of default namespace
Hi, I''m trying to run the "validates_inclusion_of" method in a before_save hook, because the range is dependant on the related data. But I can''t figure out how to call it. When calling it normally, it says it can''t find it. I''ve tried several combinations like "ActiveRecord::Validations.validates_inclusion_of", but I can''t...
2006 Jan 25
7
join fields for list views
I am sitting with the Agile book on my desk and scratching my head at the discussion on aggregation - perhaps that isn''t what I need. I have a db called clients. fields include - first_name middle_initial last_name I want to combine them all into one name element in a list view. I can add/edit the fields separately but in the list view, I only want the one combo field. Is there a