I recommend creating a virtual attribute instead of overriding the author method class Book Name if author end end Then you could do cool things like apply it to a form field %= f. Text_field :author_name % Would this work for your situation?
I recommend creating a virtual attribute instead of overriding the author= method. Class Book Would this work for your situation?
I thought to make this, but I didn't want duplicate attributes. The method I proposed above is working very well; I just wanted to share it. I can indeed make the text_field trick with it.
But thanks for your reply! =D – Lailson Bandeira Aug 16 '09 at 1:04 Wouldn't it be more correct to replace the author_name method with delegate :name, :to => :author, :prefix => true? – Adam Lassek Apr 20 '10 at 0:50 1 @Adam, That is certainly an alternative way to do it.
I usually only use delegate when dealing with multiple methods. If there's only one I prefer defining the method directly because I feel it's more clear. – ryanb Apr 20 '10 at 18:09 Are there any hidden downsides to using delegate, or is this just personal preference?
– Adam Lassek Apr 20 '10 at 21:07 I can't think of any downsides to using delegate. It just feels a little more complicated than defining the method directly in this specific scenario. – ryanb Apr 20 '10 at 20:03.
When you override the accessor, you have to set an actual DB attribute for write_attribute and self:the_attribute=, and not the name of the association-generated attribute you're overriding. This works for me. Require 'rubygems' require 'active_record' ActiveRecord::Base.
Establish_connection(:adapter => "sqlite3", :dbfile => ":memory:") ActiveRecord::Schema. Define do create_table(:books) {|t| t. String :title } create_table(:authors) {|t| t.
String :name } end class Book author_name) end end end class Author "John Doe") Author. Create!(:name => "Tolkien") b1 = Book. New(:author => "John Doe") p b1.
Author # => # b2 = Book. New(:author => "Noone") p b2. Author # => # b2.
Save p b2. Author # => # I strongly recommend doing what Ryan Bates suggests, though; create a new author_name attribute and leave the association generated methods as they are. Less fuzz, less confusion.
As I said above, the method you proposed works only if the author is save (i.e. Has an id), which is not my case. I can have a new author that must be saved only when the book is saved too.
– Lailson Bandeira Aug 16 '09 at 1:07 I re-wrote it a bit based on your comment. Does it make more sense now? – August Lilleaas Aug 16 '09 at 7:34 Oh thanks, now this works as I expect.
But (in spite of all recommendations) I'll keep the original solution. Nonetheless, it's a good alternative if I change my mind someday. = – Lailson Bandeira Aug 16 '09 at 12:25.
I hope this can be useful to others facing the same problem. Let me explain with an example. But, for whatever reason, you need to override the author= method on Book.
As I'm new to Rails, I've followed the Sam Ruby's suggestion on Agile Web Development with Rails: use attribute_writer private method. Unfortunately, this does not work.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.