Ruby on rails rails控制器中的扩展方法
这可能是非常规的,但是否可以扩展控制器中的任何has\u one方法,例如Ruby on rails rails控制器中的扩展方法,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,这可能是非常规的,但是否可以扩展控制器中的任何has\u one方法,例如association=(associate)。我已经在模型中看到了这一点,但是否可以在控制器中实现?如果是,如何实现 解释一下我的意思 从 关联方法是在模型类中包含的模块中生成的,该模块允许您轻松地使用自己的方法进行重写,并使用super调用原始生成的方法。例如: class Car < ActiveRecord::Base belongs_to :owner belongs_to :old_owner de
association=(associate)
。我已经在模型中看到了这一点,但是否可以在控制器中实现?如果是,如何实现
解释一下我的意思
从
关联方法是在模型类中包含的模块中生成的,该模块允许您轻松地使用自己的方法进行重写,并使用super调用原始生成的方法。例如:
class Car < ActiveRecord::Base
belongs_to :owner
belongs_to :old_owner
def owner=(new_owner)
self.old_owner = self.owner
super
end
end
控制对关联的访问的代理对象可以通过匿名模块进行扩展。这对于添加新的查找者、创建者和其他仅作为此关联的一部分使用的工厂类型方法尤其有益
class Account < ActiveRecord::Base
has_many :people do
def find_or_create_by_name(name)
first_name, last_name = name.split(" ", 2)
find_or_create_by(first_name: first_name, last_name: last_name)
end
end
end
person = Account.first.people.find_or_create_by_name("David Heinemeier Hansson")
person.first_name # => "David"
person.last_name # => "Heinemeier Hansson"
用户模型
class User < ActiveRecord::Base
has_one :profile
has_many :teammates, :class_name => "User", :foreign_key => "captain_id"
belongs_to :captain, :class_name => "User"
# before_create :build_profile
after_create :build_default_profile
accepts_nested_attributes_for :profile
attr_accessible :email, :password, :password_confirmation, :profile_attributes
def build_default_profile
Profile.create(user_id: self.id)
end
class用户“User”,:foreign\u key=>“captain\u id”
属于:船长,:class\u name=>“用户”
#创建前:生成配置文件
创建后:生成默认配置文件
接受\u嵌套的\u属性\u:profile
属性可访问:电子邮件、:密码、:密码\u确认、:配置文件\u属性
def生成默认配置文件
Profile.create(用户标识:self.id)
终止
结束
团队控制员
class TeamsController < ApplicationController
def new
@team = Team.new
end
def create
@team = Team.new(params[:team])
if @team.save!
flash[:success] = "Team created."
redirect_to @team
else
flash[:error_messages]
render 'new'
end
end
def index
@teams = Team.paginate(page: params[:page])
end
def show
@team = Team.find(params[:id])
end
def edit
@team = Team.find(params[:id])
end
def update
@team = Team.find(params[:id])
if @team.update_attributes(params[:team])
flash[:success] = "Team Updated"
redirect_to @team
else
render 'edit'
end
end
def destroy
Team.find(params[:id]).destroy
flash[:success] = "Team is deleted."
redirect_to teams_url
end
private
def team_params
params.require(:team).permit(:teamname, :color)
end
end
class teamscocontroller
您的问题不清楚。控制器没有关联,它们是控制器。那么你到底想做什么呢?我正试图利用控制器模型中的has_-one关联的扩展功能。你能展示你的has_-one关联模型吗?你想在控制器中实现什么?@dgilperez,我已经添加了相关的模型。
attr_accessible :teamname, :color, :result_attributes
after_create :build_result_table
after_create :build_default_captain
accepts_nested_attributes_for :profiles
accepts_nested_attributes_for :captain
accepts_nested_attributes_for :result
def build_result_table
Result.create(result_table_id: self.id, result_table_type: self.class.name)
end
def build_default_captain
captain.create(team_id: self.id)
end
end
class User < ActiveRecord::Base
has_one :profile
has_many :teammates, :class_name => "User", :foreign_key => "captain_id"
belongs_to :captain, :class_name => "User"
# before_create :build_profile
after_create :build_default_profile
accepts_nested_attributes_for :profile
attr_accessible :email, :password, :password_confirmation, :profile_attributes
def build_default_profile
Profile.create(user_id: self.id)
end
class TeamsController < ApplicationController
def new
@team = Team.new
end
def create
@team = Team.new(params[:team])
if @team.save!
flash[:success] = "Team created."
redirect_to @team
else
flash[:error_messages]
render 'new'
end
end
def index
@teams = Team.paginate(page: params[:page])
end
def show
@team = Team.find(params[:id])
end
def edit
@team = Team.find(params[:id])
end
def update
@team = Team.find(params[:id])
if @team.update_attributes(params[:team])
flash[:success] = "Team Updated"
redirect_to @team
else
render 'edit'
end
end
def destroy
Team.find(params[:id]).destroy
flash[:success] = "Team is deleted."
redirect_to teams_url
end
private
def team_params
params.require(:team).permit(:teamname, :color)
end
end