Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # app/models/artist.rb
- class Artist < ActiveRecord::Base
- # Relationships
- has_many :releases
- has_many :songs, :through => :releases
- has_many :featured_songs, :through => :releases,
- :class_name => "Song",
- :source => :song,
- :conditions => { 'releases.featured', true }
- end
- ruby-1.9.2-p180 :004 > a = Artist.first
- ruby-1.9.2-p180 :005 > a.featured_songs.create(:title => "Title", :user => User.first)
- ruby-1.9.2-p180 :004 > a = Artist.first
- ruby-1.9.2-p180 :005 > a.featured_songs.create(:title => "Title", :user => User.first)
- User Load (0.9ms) SELECT `users`.* FROM `users` LIMIT 1
- SQL (1.0ms) BEGIN
- 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]]
- 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]]
- (0.1ms) COMMIT
- class Song < ActiveRecord::Base
- has_many :releases
- has_many :artists, :through => :releases
- accepts_nested_attributes_for :releases
- end
- a.songs.create(:title => 'Title', :user => User.first, :releases_attributes => [{ :id => 123, :featured => true}])
- class User < ActiveRecord::Base
- belongs_to :address
- accepts_nested_attributes_for :address
- end
- class Address < ActiveRecord::Base
- has_one :user
- end
- u = User.new
- u.address.build({:city => "bozoville", :zip => "22222", ...})
- u.save!
Add Comment
Please, Sign In to add comment