Guest User

Untitled

a guest
Jul 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. # app/models/artist.rb
  2. class Artist < ActiveRecord::Base
  3. # Relationships
  4. has_many :releases
  5. has_many :songs, :through => :releases
  6. has_many :featured_songs, :through => :releases,
  7. :class_name => "Song",
  8. :source => :song,
  9. :conditions => { 'releases.featured', true }
  10.  
  11. end
  12.  
  13. ruby-1.9.2-p180 :004 > a = Artist.first
  14. ruby-1.9.2-p180 :005 > a.featured_songs.create(:title => "Title", :user => User.first)
  15.  
  16. ruby-1.9.2-p180 :004 > a = Artist.first
  17. ruby-1.9.2-p180 :005 > a.featured_songs.create(:title => "Title", :user => User.first)
  18. User Load (0.9ms) SELECT `users`.* FROM `users` LIMIT 1
  19. SQL (1.0ms) BEGIN
  20. SQL (5.5ms) INSERT INTO `songs` (`created_at`, `title`, `updated_at`, `user_id`) VALUES (?, ?, ?, ?) [["created_at", Thu, 11 Aug 2011 18:30:34 UTC +00:00], ["title", "Title"], ["updated_at", Thu, 11 Aug 2011 18:30:34 UTC +00:00], ["user_id", 1]]
  21. SQL (1.2ms) INSERT INTO `releases` (`album_id`, `artist_id`, `created_at`, `featured`, `song_id`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?) [["album_id", nil], ["artist_id", 1], ["created_at", Thu, 11 Aug 2011 18:30:34 UTC +00:00], ["featured", nil], ["song_id", 6], ["updated_at", Thu, 11 Aug 2011 18:30:34 UTC +00:00]]
  22. (0.1ms) COMMIT
  23.  
  24. class Song < ActiveRecord::Base
  25. has_many :releases
  26. has_many :artists, :through => :releases
  27. accepts_nested_attributes_for :releases
  28. end
  29.  
  30. a.songs.create(:title => 'Title', :user => User.first, :releases_attributes => [{ :id => 123, :featured => true}])
  31.  
  32. class User < ActiveRecord::Base
  33. belongs_to :address
  34.  
  35. accepts_nested_attributes_for :address
  36. end
  37.  
  38. class Address < ActiveRecord::Base
  39. has_one :user
  40. end
  41.  
  42. u = User.new
  43. u.address.build({:city => "bozoville", :zip => "22222", ...})
  44. u.save!
Add Comment
Please, Sign In to add comment