search for: find_or_initialize_by_nam

Displaying 3 results from an estimated 3 matches for "find_or_initialize_by_nam".

2011 Aug 17
1
has_many :through, collection<<(object) and duplicates.
...has_many :tags, :through => :taggings, :uniq => false (:uniq => false is the default value, I''ve left it to insist on that point) Now let''s say I create a new Object and save it. In an after_save callback on Object, I add a few tags: self.tags << Tag.find_or_initialize_by_name("hey") self.tags << Tag.find_or_initialize_by_name("ho") self.tags << Tag.find_or_initialize_by_name("hey") So, twice the same tag "hey" and another one "ho". Nothing fancy. Once this is done though, I''ll get...
2009 Aug 17
1
Problem with setter override on ActiveRecord
...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. So, my first try was: class Book < ActiveRecord::Base belongs_to :author def author=(author) author = Author.find_or_initialize_by_name(author) if author.is_a? String self.write_attribute(:author, author) end end Unfortunately, this does not work. That''s what I get from console: >> book = Book.new(:name => "Alice''s Adventures in Wonderland", :pub_year => 1865) => #<Book id: nil...
2006 Jul 02
0
Rails Core Weekly June 19 - July 2 2006
...rails --version is now documented: http://dev.rubyonrails.org/changeset/4471 Sam Stephenson added a find_or_initalize_by_x similar to find_or_create_x except that initialize does not save the record just yet. From his documentation: # # No ''Winter'' tag exists # winter = Tag.find_or_initialize_by_name("Winter") # winter.new_record? # true Another example is found in the test he wrote. This uses 2 attributes to search on: def test_find_or_initialize_from_two_attributes another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John") assert_e...