Displaying 20 results from an estimated 5000 matches similar to: "write_attribute functionality for related objects?"
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)
2006 Apr 14
1
Validating Existing Models
I''ve got a user object which has quite a few attributes mapped to the
database. As of right now I''ve got some validations in the model like:
validates_presence_of :password, :message => "You must specify a valid
password."
validates_confirmation_of :password, :message => "Password doesn''t match
the confirmation."
validates_presence_of
2006 Nov 04
0
write_attribute trouble
Can anyone clarify something for me? I''ve been having a hard time with
write_attribute.
If I do (for instance in a callback):
write_attribute(:user, user)
Is obj.user_id set automatically too? And viceversa, if I do:
write_attribute(:user_id, user.id)
Does that set obj.user for me?
Doesn''t seem like it''s doing that for me.
Anyone understands exactly what
2005 Apr 23
7
Validation question
Hi all,
Is there a way to invoke validations at times other than save, create
and update? I know that I can do this by writing my own validation
checks using errors.add_[blah], but I''d like to leverage the existing
validation code.
What I have is two sets of fields in a record, set A and set B. Both
sets must be validated on record create. However, the trouble is that
after
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
2006 Jul 12
2
Weird problem. How to tell if an attribute has changed? Please help.
How do you tell if an foreign key attribute has been changed?
For example, lets say you have a model with attribute table_a_id.
Would you do this?
def table_a_id=(value)
if value != table_a_id
@table_a_changed = true
write_attribute ''table_a_id'', value
end
end
def table_a=(value)
if value != table_a
@table_a_changed = true
2007 Dec 13
2
automatically strip values of activerecord attributes
i am doing a couple of times the following in my models
class User < ActiveRecord::Base
def name=(val)
write_attribute :name, val.to_s.strip
end
end
quite clear, it strips the name when setting it.
now the question: Is it possible to do something like the following ..
and if yes how could i achieve it
class User < ActiveRecord::Base
auto_strip :name, :street
end
thanks...
--
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
2007 Nov 26
6
Model setters, override attribute=(attribute)?
Hi,
I have a model similar Basket shown below. If customer_type is set
then it should also set customer to nil. Likewise, if I set customer,
it should inspect the customer_type attached to the customer and set
accordingly within Basket. However implementing it as below doesn''t
seem to work.
Am I missing a better way to achieve this? I had also considered using
a before_save
2007 May 23
6
RESTful RESOURCE PARTIAL UPDATE
I have modified scaffolding code slightly to get three edit actions
and three update actions.
The requirement: to allow users to edit attributes for the same model
from seperate forms instead of one form, while using the given
scaffolding code and performing model validations. I am using
restful_authentication with the login email password validations
defined.
class users_controller
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 Mar 13
3
validates_associated problem
Hello,
I have a situation where an ''employee'' belongs to a ''department'' and
have setup the relationship as follows.
class Employee < ActiveRecord::Base
belongs_to :department, :foregin_key => "department_id"
validates_associated :department_id
end
class Department < ActiveRecord::Base
has_many :employees
def validate_on_update
2006 Jun 01
1
Automatted Getter/Setter
Hey..
So I have a model that has a lot of number fields. Each of these go
through a process when read in and read out to add/remove commas
(between every three numbers) to make them more readable.
The getter I currently have is like this:
def sale_price
Listing.insert_commas(read_attribte (:sale_price))
end
and the setter is:
def sale_price=(price)
write_attribute(:saleprice,
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> |
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 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 May 07
1
Overwriting setters for associated objects
Hi all,
I''m trying to overwrite a setter method, but am running into some
difficulties. This is normally really easy when overwriting a setter for
a table column, but in this case it is the setter for an associated
object.
Here''s a hypothetical example:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
2009 Jul 13
1
[PATCH] Use volume key instead of path to identify volume.
This patch teaches taskomatic to use the volume 'key' instead of the
path from libvirt to key the volume off of in the database. This fixes
the duplicate iscsi volume bug we were seeing. The issue was that
libvirt changed the way they name storage volumes and included a local
ID that changed each time it was attached.
Note that the first run with this new patch will cause duplicate