Issue with setting values to a model in rails
I have a model named User . I am using Devise for authentication. The 'User' has many 'Profiles' and one default profile . So I have added a column called 'default' to the User model. I want to store the id of the default profile. However the code fails at the current_user.default = statement.
Error - undefined method `default=' for #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
How do I go about this ? Adding 'default' to the开发者_JAVA百科 User model doesnt solve the problem.
I suggest you to use STI, it means add "type" column into 'profiles' table
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
精彩评论