A Feed has many Subscriptions
class Feed < ActiveRecord::Base
has_many :subscriptions
end
class Subscription < ActiveRecord::Base
belongs_to :feed, :counter_cache => true
belongs_to :user
end
This does not work
ch = Channel.find(:first)
ch.feeds.create(:user_id => 1, :score => 0.0)
user_id and score columns must be non-null, SQL INSERT sets them NULL.
Nor does it work with string keys instead of symbols.
This does:
ch = Channel.find(:first)
ch.feeds.create do |f|
f.user_id = 1
f.score = 0.0
end
According to AWDWR 1st and http://rails.rubyonrails.org/ both should behave
the same. What has changed or I am doing wrong.
TIA,
Jeffrey
It''s possible that the attributes aren''t getting assigned because they''re protected. Do you have attr_protected or attr_accessible declared anywhere in your model? On Jun 30, 7:19 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> A Feed has many Subscriptions > > class Feed < ActiveRecord::Base > has_many :subscriptions > end > > class Subscription < ActiveRecord::Base > belongs_to :feed, :counter_cache => true > belongs_to :user > end > > This does not work > ch = Channel.find(:first) > ch.feeds.create(:user_id => 1, :score => 0.0) > > user_id and score columns must be non-null, SQL INSERT sets them NULL. > Nor does it work with string keys instead of symbols. > > This does: > ch = Channel.find(:first) > ch.feeds.create do |f| > f.user_id = 1 > f.score = 0.0 > end > > According to AWDWR 1st andhttp://rails.rubyonrails.org/both should behave > the same. What has changed or I am doing wrong. > > TIA, > Jeffrey
This is on Rails 2.3.2. Quoting Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org>:> > A Feed has many Subscriptions > > class Feed < ActiveRecord::Base > has_many :subscriptions > end > > class Subscription < ActiveRecord::Base > belongs_to :feed, :counter_cache => true > belongs_to :user > end > > > This does not work > ch = Channel.find(:first) > ch.feeds.create(:user_id => 1, :score => 0.0) > > user_id and score columns must be non-null, SQL INSERT sets them NULL. > Nor does it work with string keys instead of symbols. > > > This does: > ch = Channel.find(:first) > ch.feeds.create do |f| > f.user_id = 1 > f.score = 0.0 > end > > According to AWDWR 1st and http://rails.rubyonrails.org/ both should behave > the same. What has changed or I am doing wrong.
Pat, Head slap, yes that is precisely the problem. Thanks for seeing into my blind spot. Jeffrey Quoting Pat Nakajima <patnakajima-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > It''s possible that the attributes aren''t getting assigned because > they''re protected. Do you have attr_protected or attr_accessible > declared anywhere in your model? >[snip]