0

我有四个用户模型:Zone, Product, User,Group 我想选择什么s 可以在 aUser中出售 a ,这就是它的作用,与 one和 one具有多对多关系和外键。所以我每对区域/产品有一组。我还需要在多对多关系上设置自定义属性,所以我使用了(我找不到更好的名称来描述和之间的关系)。所以我最终有 5 个模型:, , , , .ProductZoneGroupUserProductZonehas_many :through SellGroupUserZoneProductUserGroupSell

它工作正常,但现在我需要选择Group. 我正在考虑利用 Sell.id 来查找分配到具有更高 id 的同一组的用户,如果不存在,请再次选择第一个(这允许我创建一个环链)。Group.next_user有一个方法会很有用。

不幸的是,我不知道如何做到这一点,我需要帮助才能找到组中的下一个可用用户(如果没有更多用户,则为第一个)。

遵循模型所有模型的代码:

################
# models/group.rb
################
class Group < ActiveRecord::Base
  has_many :sells
  has_many :users, :through => :sells

  belongs_to :zone
  belongs_to :product
  attr_accessible :priority, :product_id, :user_ids, :zone_id
end

################
# models/zone.rb
################
class Zone < ActiveRecord::Base
  belongs_to :location
  has_many :cities
  has_many :groups

  attr_accessible :name, :location_id
  validates :location, :presence => true
end

################
# models/user.rb
################
class User < ActiveRecord::Base
  after_create :create_calendar
  before_destroy :destroy_calendar
  belongs_to :location
  belongs_to :mall
  has_one :event_calendar

  has_many :sells
  has_many :groups, :through => :sells

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable, :registerable,
  # :recoverable, :rememberable,
  devise :database_authenticatable, :trackable,
         :validatable, :authentication_keys => [:username]

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :password, :password_confirmation, :remember_me, :name,
                  :surname, :role, :location_id
  # attr_accessible :title, :body

  ROLES = %w[Admin Agente Hostess HostessAdmin]

  validates_uniqueness_of :username, :case_sensitive => false
  validates :username, :presence => true
  validates_presence_of :role, :name, :surname, :location_id
  validates :location, :presence => true
    validates :role, :inclusion => { :in => ROLES, :message => "%{value} non è un ruolo valido." }

  def display_name
      "#{self.name} #{self.surname}"
  end

  def has_role?(role)
    # convert the role string to a sybmol
    self.role.downcase.gsub(/\s+/, "_").to_sym == role
  end

  private
  def create_calendar
    if self.has_role? :agente
      calendar = EventCalendar.new({:user_id => self.id})
      calendar.save()
    end
  end

  def destroy_calendar
    if self.has_role? :agente
      calendar = EventCalendar.find_by_user_id(self.id)
      calendar.destroy()
    end
  end

  def email_required?
    false
  end

  def email_changed?
    false
  end
end

################
# models/product.rb
################
class Product < ActiveRecord::Base
  after_create :create_groups
  before_destroy :destroy_groups
  attr_accessible :name

  def create_groups
    for zone in Zone.all
      group = Group.new({:zone_id => zone.id, :product_id => self.id})
      group.save()
    end
  end

  def destroy_groups
    for zone in Zone.all
      group = Group.find_by_product_id(self.id)
      group.destroy
    end
  end
end

################
# models/sell.rb
################
class Sell < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

你能给我一些帮助来完成这件事吗?谢谢!

4

1 回答 1

1

如果我做对了,那么将其添加到您的用户模型中

scope :next, lambda { |p| {:conditions => ["id > ?", p.id], :limit => 1, :order => "id"} }

这对你的团体模型

def self.next_user
  return User.first if Group.users.blank?
  next_user = User.next(Group.users.last).first
  return next_user || Group.users.first
end

这应该可以解决问题。我没有为此编写测试,因此您应该对其进行测试:)

于 2012-05-16T12:15:46.393 回答