Saving connection attributes via has_many: through: conditions

I have an Artist model that looks like this:

# 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

Extracting featured_songs works great. The problem here is that I cannot add a new featured_song to the artist, because for some reason the attribute 'featured' is set to 'nil'.

This is what I am trying:

ruby-1.9.2-p180 :004 > a = Artist.first
ruby-1.9.2-p180 :005 > a.featured_songs.create(:title => "Title", :user => User.first)

Actual result:

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

Note: ["featured", nil]

Any idea what I'm doing wrong? How can I set attributes in my connection correctly without accessing it directly?

Thank you!

EDIT: To make my problem more clear:

  • From an artist instance I cannot create new recognized songs through a featured_songsrelationship

  • Saving seems to set all EXCEPT song attributes for (most important) featured

  • - featured nil, .

+5
4

:

https://github.com/rails/rails/issues/5178#issuecomment-4181551

has_many , , has_many: .

, :

class Artist < ActiveRecord::Base
  # Relationships
  has_many  :releases
  has_many  :songs, :through => :releases

  has_many  :featured_releases, :class_name => "Release", :conditions => { :featured => true }
  has_many  :featured_songs, :through => :featured_releases, :source => :song
end
+2

EDIT: : releases_attributes, has_many .

jaydel . , accepts_nested_attributes_for. has_many , .

, , , . , , , releases.

accepts_nested_attributes_for - . ( ), Song.

class Song < ActiveRecord::Base
  has_many :releases
  has_many :artists, :through => :releases
  accepts_nested_attributes_for :releases
end

create :

a.songs.create(:title => 'Title', :user => User.first, :releases_attributes => [{ :id => 123, :featured => true}])

_. , Rails , . ActiveRecord nested_attributes.rb.

, Song#create_featured, :releases_attributes #create.

0

, , attr_accessable .

Release:

attr_accessible :song_id, :feature_id
0

accepts_nested_attributes_for . Railscast ,

EDIT:

, , . , , . , :

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!

. , ? , , , , . , , , , , ? , ...

-1

All Articles