Проблема с настройкой значений для модели в рельсах - PullRequest
0 голосов
/ 18 июля 2011

У меня есть модель с именем User. Я использую Devise для аутентификации. «Пользователь» имеет много «Профилей» и один профиль по умолчанию. Поэтому я добавил столбец с именем «default» в модель User. Я хочу сохранить идентификатор профиля по умолчанию. Однако код не выполняется в операторе current_user.default =.

Ошибка - неопределенный метод `default = 'для #User: 0x402c480

class User < ActiveRecord::Base

..........
has_many :profiles
...........

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :default

end

.......

class ProfilesController < ApplicationController

before_filter :authenticate_user! ,:except => [:show]



  def create
    @profile = current_user.profiles.new(params[:profile])
    @profile.user = current_user

    respond_to do |format|
      if @profile.save
      @current_user.default= @profile.id
       ............


end

Как мне это сделать? Добавление 'default' к модели User не решает проблему.

1 Ответ

1 голос
/ 18 июля 2011

Я предлагаю вам использовать STI, это означает добавить столбец «type» в таблицу «profile»

class User < ActiveRecord::Base
  has_many :profiles
  has_one  :profile_default

  after_create do
    create_profile_default
  end
end

class Profile < ActiveRecord::Base
end

class ProfileDefault < Profile
end


def create
  @profile = current_user.profiles.new(params[:profile])
  @profile.user = current_user

  respond_to do |format|
    if @profile.save
    ...
end
...