similar to: Overriding column accessors?

Displaying 20 results from an estimated 2000 matches similar to: "Overriding column accessors?"

2009 Sep 23
5
Overriding AR read/write_attribute - Overridden write_attribute Is Never Called
Could someone explain this? #config/initializers/ar_attributes.rb module ActiveRecord module AttributeMethods alias_method :ar_read_attribute, :read_attribute def read_attribute(attr_name) p "read_override" ar_read_attribute(attr_name) end alias_method :ar_write_attribute, :write_attribute def write_attribute(attr_name, value)
2010 Jul 19
3
ActiveRecord override
Would really appreciate it if someone could tell me why this is happening: I override ssn method of AR class so I can encrypt/decrypt to/from the db: class Person < ActiveRecord::Base def ssn=(value) write_attribute(:borrower_ssn, Crypto.encrypt(self.encryption_key, value)) write_attribute(:borrower_ssn_final_four, value[5,4]) end def ssn # this begin - rescue can be
2007 Mar 30
1
write attribute coming from where?
There are datatypes that need to be converted back and forth from a string representation: they need to be parsed both coming from the database, and coming from elsewhere like views. Dates are an example, and I recently needed to implement a more forgiving converter for decimal numbers. So we have here at least two flows of execution, for example: post.date = "22-jan-2007"
2006 Jul 14
1
Weird validation problem
I have a field called Fax in my model. I store it as a 10-digit number with no other characters but I display it as a nicely formatted phone number. I have a validation on it like this: validates_format_of :Fax, :message => ''number should be formatted using (XXX) XXX-XXXX'', :with => /^\(\d{3}\)\ ?\d{3}-\d{4}$/ If I put 1234567890 (10 digits) in my field, the
2009 Aug 17
1
Problem with setter override on ActiveRecord
(This message was originally written on StackOverflow -- the formatting is much prettier there -- http://stackoverflow.com/questions/1283046/problem-with-setter-override-on-activerecord) This is not exactly a question, it''s rather a report on how I solved an issue with write_attribute when the attribute is an object, on Rails'' Active Record. I hope this can be useful to others
2006 Feb 06
7
Delaying initialization of associations until first access
Guys, Say I have the following: ----- class Person < ActiveRecord::Base has_one :house end class House < ActiveRecord::Base belongs_to :person attr_accessor :color end ----- Then I have the following code: ----- john = Person.new john.house.color = "Blue" john.save ----- What I would like to have happen, is that on first call to john.house, if house hadn''t been
2006 Jul 06
6
Help with overriding / overwriting date values in ActiveRecord to accept Euro Style Date from a Textbox
Hi guys, I''m trying to make an AR model which accepts a string data (from a text input) in euro date format eg <input .... value = "23/01/2006"> ( 23 Jan 2006) Reading the example from RDoc... i tried to overwrite/overload/override the = function to write in the date.. Cant get it to work because .. i dont knoe what format should it accept.. i''ve tried
2006 Aug 19
5
Form helpers don''t work with facade columns?
Hello all, I''ve been trying to use a facade column but I notice that the form helpers don''t seem to respect the facade. In the following example I''m trying to story the prices as Integer values but present them to the user as decimals. class Product < ActiveRecord::Base def price=(new_price) write_attribute(:price, Float(new_price)*100) end def price
2006 Jun 13
0
Best way to format attributes during visualization/editing
Hi to everyone, My name''s Carmine and I''m a rails newbie. So, here''s my newbie question :) I have a model with one attribute named "kind" of type integer. This attribute can assume values within the range [0..5]. Each value has a "verbose" representation e.g.: 0 => grain, 1 => liquid ... To visualize the verbose representation I''ve
2006 Jul 12
1
odd habtm behavior? or is it me?
Hello List, I''m a bit spent on this problem, and my code might be whack. I seem to be encountering a problem where the primary key on a join table is not being resolved correctly. I will elaborate after some code bits. I am creating the join table using migrations, and relevant model code is this: class Post < ActiveRecord::Base has_and_belongs_to_many :records
2006 May 22
2
Inserting data in a table with a varchar key field
Hi all, I have some problems inserting data into a legacy table. the key field of this table is a field named CIN which is a varchar(15). when I try insert into this table using a form (generated by the scaffold generator) I get this Error : _____________________________________________________________________ undefined method `CIN_before_type_cast'' for #<Candidat:0x370d8c8> |
2006 May 10
13
Issue in ActiveRecord generated reader methods
Hi all! I have come into a strange issue in Rails that I am hoping someone can shed some light. To make a very long story short, I have been researching how to override ActiveRecord::Base#read_attribute and write_attribute to perform security checks at the model level (influenced by the ModelSecurity generator). Shortly after implementing some code to check this behavior, I began to
2006 May 10
0
Saving extracted data
Hi there, I have a file upload form on my page that is for images. In the model I have this: def file_data=(file_data) @file_data = file_data write_attribute ''extension'', file_data.original_filename.split(''.'').last.downcase write_attribute("content_type", file_data.content_type) write_attribute("current_name",
2006 Mar 21
13
"Remembering" link to redirect to after logging in
Hi, Suppose I have a resource such as http://localhost:3000/topsecret/data that requires the user to login first. What I did was make use of before_filter to check and see if the session variable is set with the logged in user''s id (similar to the example in "Agile Web Development with Rails"). However while the filtering function does work correctly in redirecting the
2006 Feb 20
0
Override automatic accessor for reference field in model
When a column in a database table is actual data, rather than an fkey to another table, I can override the automatic accessor by just doing the following: def field_name # do something here read_attribute(field_name) end This does not seem to work for me if the field is an fkey, say user_id, linking to another table. I''ve made this work by aliasing the accessor like this: alias
2009 Mar 27
1
UTF8 postgres args saving issue
Hi, All I have an encountered an issue where the args field is not saved correctly to the database. I encounter an error like this: ActiveRecord::StatementInvalid (RuntimeError: ERROR C22021 M invalid byte sequence for encoding "UTF8": 0xcb3a H This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by
2010 Dec 30
6
validates_inclusion_of doesn't match constant
Hello, I have a model Model: SPECIAL_FEATURES = %w(none top) class Model validates_inclusion_of :special_feature, :in => Model::SPECIAL_FEATURES, :on => :create, :message => "special_feature %s is not defined" belongs_to :team def special_feature attributes = attributes_before_type_cast if attributes["special_feature"]
2006 Aug 20
0
Re: Problem with overriding rails accessor methods
On 8/20/06, Paul Corcoran <prcorcoran-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote: > > > Hello all, > > I just encountered something that is not what I would expect. I > overrode an accessor method in my model class and within this method I > did a "super" call which failed. Of couse my real method would be doing > more but here is the basic code that
2006 Feb 22
0
Rewriting attribute accessors?
I''m new to Rails and so was wondering if the following strategy is a good idea. Background: I''m writing a eCommerce application which has a Product model and an Option model, they are setup with a has_and_belongs_to_many relationship. The Product model features the ''base'' attributes of the product and then the shopkeeper *can* add one or more Options that
2008 Jan 16
4
ActionController::InvalidAuthenticityToken in LoginController
Hi, I''m having a problem trying to get a login controller working. When I try and post to my login controller I get the following error: ActionController::InvalidAuthenticityToken in LoginController#login login_controller: class LoginController < ApplicationController def login case request.method when :post if @session[''user''] =