Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

ruby on rails - Beekeeper need your help for relationship choice

I'm a beekeeper and writing an app under RoR to follow my beehives. Here are the main models that I have actually:

class Hive < ApplicationRecord
  has_many :moves, dependent: :destroy
  has_many :yards, through: :moves
  has_many :visits
  has_many :varroas
class Yard < ApplicationRecord
  has_many :moves, -> { where(:most_recent => true) }
  has_many :hives, through: :moves
  belongs_to :owner
  
  has_many :most_recents_moves, -> { where(:most_recent => true) }, :class_name => 'Move'
  scope :last_move_completed, -> { joins(:moves).where('moves.most_recent=?', true)}
class Move < ApplicationRecord
  belongs_to :hive
  belongs_to :yard
  
  after_create :mark_most_recent

    def mark_most_recent
      hive.moves.order(:created_at => :desc).offset(1).update_all(:most_recent => false)
      hive.moves.order(:created_at => :desc).limit(1).update_all(:most_recent => true)
    end
class Visit < ApplicationRecord
  belongs_to :hive
  belongs_to :visittype
class Varroa < ApplicationRecord
  belongs_to :hive

BUT, i need a Colony element between hives and visits or varroas.

When I visit a hive, in fact, I visit the colony. The colony can be moved to another hive for many reasons. A hive can be without colony (in stock), but a colony can not be without hive. concretely, a colony don't have any reference. When I make a visit, the only reference I have is the hive number. I should be able to access it as Hive.colony.visit.new or Hive.colony.visit.last

What is the right relationship between hives and colony ?

Thank you,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...