2011-04-23 1 views
0

다음과 같이 사용자와 마이크로 포스트 간의 간단한 일대 다 관계가 있습니다. Micropost 모델에 stage이라는 새 열을 추가하려고했습니다. 새 마이크로 포스트를 만들고 저장하려고하면 stage 열이 항상 자동으로 nil으로 설정됩니다. 내가 시도한 create, build - 상관 없어요, stage 필드는 항상 nil로 설정됩니다. 나는 당황 스럽다. 제발 도와 줘! 이 모든 것이 잘 작동하지 않고빌드를 통해 새로운 관련 객체를 생성하면 레일스에서 ​​항상 일부 열이 nil로 설정됩니다.

attr_accessor :stage 

:

$ rails console 
Loading development environment (Rails 3.0.5) 
>> User.first.microposts.create!(:stage => "p", :content => "test 6") 

=> #<Micropost id: 2, content: "test 6", stage: nil, user_id: 1, created_at: "2011-04-23 22:14:20", updated_at: "2011-04-23 22:14:20"> 

...

class Micropost < ActiveRecord::Base 
    attr_accessible :content, :stage 
    attr_accessor :stage 

    belongs_to :user 

    validates :content, :presence => true, :length => { :maximum => 140 } 
    validates :user_id, :presence => true 

    default_scope :order => 'microposts.created_at DESC' 
    scope :from_users_followed_by, lambda { |user| followed_by(user) } 

    private 
    def self.followed_by(user) 
     followed_ids = %(SELECT followed_id FROM relationships 
         WHERE follower_id = :user_id) 
    where "user_id IN (#{followed_ids}) OR user_id = :user_id", 
             { :user_id => user } 
    end 

end 

...

class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :name, :email, :password, :password_confirmation 

    has_many :microposts, :dependent => :destroy 
end 
+0

'stage'는 DB의 실제 열 또는 모델의 속성입니까? – ctcherry

답변

0

당신은 줄을 제거해야합니다. 나는 그것이 attr_accessorattr_accessible 사이의 갈등이라고 생각합니다.

관련 문제