Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecord - Find next user in group model
    text
    copied!<p>I've four user models: <code>Zone</code>, <code>Product</code>, <code>User</code>, <code>Group</code> I want to choose what <code>User</code>s can sell a <code>Product</code> in a <code>Zone</code>, this is what <code>Group</code> does, with a many to many relation to <code>User</code> and a foreign key to one <code>Product</code> and one <code>Zone</code>. So I have one group per pair Zone/Product. I will also need to set custom attributes on that many to many relation so I used <code>has_many :through Sell</code> (I was unable to find a better name to describe the relation between <code>Group</code> and <code>User</code>). So I ended up having 5 models: <code>Zone</code>, <code>Product</code>, <code>User</code>, <code>Group</code>, <code>Sell</code>.</p> <p>It works fine, but now I'd need to select the next user available in a <code>Group</code>. I was thinking to exploit Sell.id to find the user assigned to the same group with an higher id, if not present choose the first one again (this allows me to create a ring chain). It would be useful to have a <code>Group.next_user</code> method.</p> <p>Unfortunatly I can't figure out how to do this, I'd need help to find the next user available in the group (or the 1st one if there are no more users).</p> <p>Follows the code for models all the models:</p> <pre><code>################ # models/group.rb ################ class Group &lt; ActiveRecord::Base has_many :sells has_many :users, :through =&gt; :sells belongs_to :zone belongs_to :product attr_accessible :priority, :product_id, :user_ids, :zone_id end ################ # models/zone.rb ################ class Zone &lt; ActiveRecord::Base belongs_to :location has_many :cities has_many :groups attr_accessible :name, :location_id validates :location, :presence =&gt; true end ################ # models/user.rb ################ class User &lt; 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 =&gt; :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 =&gt; [: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 =&gt; false validates :username, :presence =&gt; true validates_presence_of :role, :name, :surname, :location_id validates :location, :presence =&gt; true validates :role, :inclusion =&gt; { :in =&gt; ROLES, :message =&gt; "%{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 =&gt; 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 &lt; 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 =&gt; zone.id, :product_id =&gt; 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 &lt; ActiveRecord::Base belongs_to :user belongs_to :group end </code></pre> <p>Can you give me some help to get this done? Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload