簡體   English   中英

還有其他方法可以在Ruby on Rails中編寫嗎?

[英]Is there another way to write this in Ruby on Rails?

我有以下代碼:

class Company < ActiveRecord::Base
  has_many :users, :through => :company_users do 
    def regular
      where('regular_user = ?', true)
    end

    def employee
      where('regular_user = ?', false)
    end
  end

我想知道另一種寫此方法的方法,或者這是否是最有效的方法。 我在考慮用戶模型中的范圍。 有任何想法嗎?

我將regularemployee寫為User范圍:

class Company < ActiveRecord::Base
  has_many :users, :through => :company_users 
end

class User < ActiveRecord::Base
  scope :regular, where(:regular_user => true)
  scope :employee, where(:regular_user => false)
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM