Rails 4 has_many:through关联:在其他父模型中使用devise current_user在after_create回调中

蒂博·克莱门特(Thibaud Clement)

我有三个模型,并且有一个has_many :through关联:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

连接Administration模型具有以下属性:

id
user_id
calendar_id
role

这是我目前的路线:

devise_for :users, :path => 'account'

resources :users do
  resources :calendars
end

更新

这是Calendars#create代码:

def create
    @user = current_user
    @calendar = @user.calendars.create(calendar_params)
    respond_to do |format|
      if @calendar.save
        format.html { redirect_to user_calendar_path(@user,@calendar), notice: 'Calendar was successfully created.' }
        format.json { render :show, status: :created, location: @calendar }
      else
        format.html { render :new }
        format.json { render json: @calendar.errors, status: :unprocessable_entity }
      end
    end
  end

每当auser创建新内容时calendar,我都想连接模型中为该所有者分配user一个“所有者”。roleadministration

我认为最好的方法是after_createcalendar模型中使用回调,如下所示:

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations

  after_create :set_default_role

  protected

  def set_default_role
    current_user.calendar.role = "Owner"
  end

end

但是,当我继续尝试创建新日历时,出现以下错误:

NameError in CalendarsController#create
undefined local variable or method `current_user' for #<Calendar:0x007f88f8f26998>

Extracted source (around line #10):

  def set_default_role
    current_user.calendar.role = "Owner"
  end

end

经过研究,我在Quora发现了以下问题,并给出了以下答案:

该模型直接拥有当前用户的知识不是一个好习惯;这是应用程序的“状态”或与会话相关的问题。控制器应管理当前用户。

我不得不说我有些困惑:一个通用原则是通过将逻辑放置在模型中来保持控制器的皮包骨头。但是,上面的答案建议做相反的事情,因为模型不应该知道当前用户。

因此,回到我最初的问题:当用户创建新日历时,如何为用户定义默认角色?

我应该after_create使用回调方法,还是使用其他方法而不是使用current_user他的方法id,或者使用完全不同的解决方案?

存在

已更新:该模型没有current_user。从日历模型中删除set_default_role并添加到管理模型中:

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar

  before_create :set_default_role

  def set_default_role
    self.role = "Owner"
  end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Rails 4中使用父字段创建has_many关联字段

来自分类Dev

通过Rails 4中的关联使用has_many

来自分类Dev

Rails 4 / Devise中的多表继承设置current_user

来自分类Dev

在Rails 4中使用has_many

来自分类Dev

在同一模型中的Rails关联“ has_many:through”

来自分类Dev

如何在rails 4中创建具有父字段的has_many关联字段

来自分类Dev

Rails FactoryGirl特征与模型after_create回调的关联未设置vanity_url

来自分类Dev

Rails 4中的has_many和belongs_to关联

来自分类Dev

在has_many关联中使用Rails中的单个表继承模型

来自分类Dev

在rails_admin中为has_many:through关系使用rails 4中的orderable

来自分类Dev

如何在Rails 4中使用has_many更新关系?

来自分类Dev

has_many / :through rails4

来自分类Dev

如何使用has_many / belongs_to使Rails 4关联自动保存到两个表中?

来自分类Dev

Current_user分配Rails 4

来自分类Dev

has_many通过关系可以在Rails中使用4个模型吗?

来自分类Dev

Rails Has_many和Belongs_to中的模型关联

来自分类Dev

rails-admin has_many 关联显示在模型中

来自分类Dev

使用devise和rails更改current_user的选定属性4

来自分类Dev

Rails回调中after_create,after_save和after_commit之间的区别

来自分类Dev

Rails 4 has_many关联中未定义的方法构建

来自分类Dev

通过Rails 4中的关联,联接表和has_many出现问题

来自分类Dev

Rails 4 + Devise:sign_in(user)方法不起作用,未设置current_user

来自分类Dev

Rails 4和在“ has_many” SQL中引用父ID

来自分类Dev

通过关联和嵌套模型形式使用has_many选择rails4集合

来自分类Dev

Rails 4 ActsAsTenant复合体has_many through与设置的current_tenant不兼容

来自分类Dev

在Rails中替代after_create回调的优雅替代品?

来自分类Dev

Rails4:如何在许多has_many模型中显示数据

来自分类Dev

Rails 4活动记录模型has_many通过关联吗?

来自分类Dev

Rails 4活动记录模型has_many通过关联吗?

Related 相关文章

  1. 1

    如何在Rails 4中使用父字段创建has_many关联字段

  2. 2

    通过Rails 4中的关联使用has_many

  3. 3

    Rails 4 / Devise中的多表继承设置current_user

  4. 4

    在Rails 4中使用has_many

  5. 5

    在同一模型中的Rails关联“ has_many:through”

  6. 6

    如何在rails 4中创建具有父字段的has_many关联字段

  7. 7

    Rails FactoryGirl特征与模型after_create回调的关联未设置vanity_url

  8. 8

    Rails 4中的has_many和belongs_to关联

  9. 9

    在has_many关联中使用Rails中的单个表继承模型

  10. 10

    在rails_admin中为has_many:through关系使用rails 4中的orderable

  11. 11

    如何在Rails 4中使用has_many更新关系?

  12. 12

    has_many / :through rails4

  13. 13

    如何使用has_many / belongs_to使Rails 4关联自动保存到两个表中?

  14. 14

    Current_user分配Rails 4

  15. 15

    has_many通过关系可以在Rails中使用4个模型吗?

  16. 16

    Rails Has_many和Belongs_to中的模型关联

  17. 17

    rails-admin has_many 关联显示在模型中

  18. 18

    使用devise和rails更改current_user的选定属性4

  19. 19

    Rails回调中after_create,after_save和after_commit之间的区别

  20. 20

    Rails 4 has_many关联中未定义的方法构建

  21. 21

    通过Rails 4中的关联,联接表和has_many出现问题

  22. 22

    Rails 4 + Devise:sign_in(user)方法不起作用,未设置current_user

  23. 23

    Rails 4和在“ has_many” SQL中引用父ID

  24. 24

    通过关联和嵌套模型形式使用has_many选择rails4集合

  25. 25

    Rails 4 ActsAsTenant复合体has_many through与设置的current_tenant不兼容

  26. 26

    在Rails中替代after_create回调的优雅替代品?

  27. 27

    Rails4:如何在许多has_many模型中显示数据

  28. 28

    Rails 4活动记录模型has_many通过关联吗?

  29. 29

    Rails 4活动记录模型has_many通过关联吗?

热门标签

归档