similar to: Calling a method each time an ActiveRecord Object is accessd

Displaying 20 results from an estimated 1000 matches similar to: "Calling a method each time an ActiveRecord Object is accessd"

2006 Jun 21
8
Modifying Model Output
Hi! I''m a very-very new Ruby-on-Rails user even if I have some experience with other Languages (mostly PHP, so this is my first real object-oriented language) My problem, newbie-ish as it is, is the following: I have a model, Article, that returns Articles from the DB. Now, when a certain field of the model (the intro text) contains no data, I want to fill it with a truncated
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-forum.com/.
2006 Jun 21
3
[PLUG] new plugin, acts_as_modified
Nothing special, just my first plugin. it came out of a request I saw on the list from someone who wanted to be able to tell if a model''s attributes had changed prior to being saved. Well, this plugin does just that (at least I hope it does). http://rubyforge.org/projects/actsasmodified/ Please see the README for details. Constructive comments and criticisms are appreciated. Chris
2006 Jun 26
1
plugin, acts_as_modified 1.1.2
After a couple of thoughtful comments, I''ve made changes to acts_as_modified plugin. It no longer relies on after_find and after_initialize, which were both causing performance issues. Please check it out and let me know what you think, where you might recommend improvements or additional features and what not. One feature I do have planned (again, thanks to comments), but
2007 Dec 16
4
Make AR setter methods private?
Hi, I have a AR model that I want to limit changes to be only via instance methods that I''ve added. How do I prevent my other sw from setting the instance''s attributes? I know about #attr_protected and #attr_readonly. But the first leaves the individual setters as they were and the second stops all changes. I want something like "attr_private" Thoughts? Thanks,
2006 Apr 03
1
Why no call to Model.new on find?
I created a model with some internal state derived from a combination of database and non-database data. Since I wanted to calculate these values once rather than every time they''re requested by the view, I calculated them in the initialize method and set some instance variables. However, I soon noticed that these values weren''t getting set when I retrieved data using the
2005 Nov 27
1
Comparing field's old value to new value
I want to expire caches if a certain field of an item changes. How can I compare the item field''s old value to its new value (in the create, or after_save, etc. method)? thanks csn __________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com
2006 Jul 07
3
quick send_data question...
Hi, I am using send_data to allow users to download files from the database but ran into a small problem, it seems that files upload fine but when they download only 64kb of the file is sent. I''m using the basic rails code from the Agile book: #upload code in my sheet model def file=(document_field) self.filename = base_part_of(document_field.original_filename)
2012 Dec 24
4
Bad idea to add/remove associations in after_find
I have 2 distinct types of users (artists and curators) in my system and they currently share all associations (eg has_one :portfolio). However, a curator shouldn''t have a portfolio so I would like to only add that association when required. Obviously I could just return nil for that method, but it doesn''t feel right having that there in the first place. What''s the best
2006 Apr 18
12
Formatting data drawn from a DB
Question for all: Right now i have a Table in a mySQL DB that has a row called Ingredients. When the data is entered into the DB its enter like so from a text area: 1 1/2 lbs. beef top sirloin, thinly sliced 1/3 cup white sugar 1/3 cup rice wine vinegar 2 tablespoons frozen OJ concentrate 1 teaspoon salt 1 tablespoon soy sauce 1 cup long grain rice 2 cups water 1/4 cup cornstarch 2 teaspoons
2006 Aug 16
6
modeling ''phony'' fields.
Hi, I have a user model with an e-mail field. When a user registers for the site, I want them to confirm their email address by typing it in again. If they change their email address, I want them to confirm their email address by typing it in again. However, the user might change things on their account not related to their email address, such as their phone number, and they should not have
2006 Aug 27
3
n-to-m relationships in Forms
Hi, I can''t understand the RoR-API concerning certain form helpers. Can you maybe help me? I have a model, Film, with a habtm-relationship to the model Language. Now if I want to have a form for editing/creating a Film, I''d like to have checkboxes for each Language with those checked that are associated with the Film in question. Is there an "easy" way to do this
2006 Feb 11
5
after_(read|find) callback?
I am pondering the possibility of encrypting/decrypting some fields in a SQLite backend on-the-fly. The point of the message is not security, I know that''s broken, but whether there''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
2006 Aug 12
7
Collection assignment to a has_many :through
I''m working on a simple photo gallery in rails, it seems to be a good project for a newbie. I have photos and categories, many-to-many association. It worked well with HABTM. Then I decided that it would be good to be able to change order of the photos so that thumbnail pages would look less chaotic. So I created a Layout model which is a join model (or whatever it is called) that
2006 Jul 05
2
Serialized object behaves weird
Hi! I got a class named EinsatzFilter which I serialized to session. Before saving to session it works afterwards I keep getting the message: "undefined method `to_s'' for #<Person:0x38c6ab8>". "Person" is a from ActiveRecord::Base inherited class. Code: class EinsatzFilter include ApplicationHelper attr_reader :personen, :monat, :projekte, :kunde
2006 Jun 08
3
has_many :through updates on delete.
I have a model that looks like this: class Actor < ActiveRecord::Base has_many :member_groups, :foreign_key => ''member_id'', :class_name => ''GroupMember'' has_many :groups, :through => :member_groups, :source => :group end class Group < Actor has_many :group_members, :foreign_key => ''group_id'',:dependent =>
2007 Mar 04
4
Rails functional testing and Mocha
I''ve always wanted to be able to do stuff like this in my functional tests c = customers(:customer_1) c.expects(:great_customer_service) post :service_customer, :id => c.id This of course fails because inside the rails action a different instance of customer is used. Some of the time setting your expectation/stubbing on Customer.any_instance works, but it''s not
2006 Oct 08
1
Two-way eager loading?
My problem is that I have two classes where the eager loading isn''t working as well as I would hope... Here are my two classes: class Daynote < ActiveRecord::Base belongs_to :userplace class Userplace < ActiveRecord::Base has_many :daynote, :dependent => true When I show my userplaces, I want to get the associated daynotes with them, so I am doing: @userplace =
2006 Jul 28
11
Forum
Im making forum and i have problem with paths. Here is code: http://pastie.caboo.se/6389 With this code, it works. But its a bit weird to use so many paths function. For every path i need to make new function. So can you suggest me something. Tables are like here: http://wiki.rubyonrails.com/rails/pages/ForumExample -- Posted via http://www.ruby-forum.com/.
2006 Jul 27
25
Why are has_one objects resaved when the parent is saved?
If the associated object of a has_one association has been loaded, it is resaved when the parent is saved. Eg: class Person < ActiveRecord::Base has_one :user end class User < ActiveRecord::Base belongs_to :person end p = Person.find(:first) p.save # As expected, nothing happens with the user association p.user # Loads the associated object p.save # As well as saving the person, the