铁路协会选项

发布于 2025-01-07 11:50:39 字数 1453 浏览 1 评论 0原文

我是 Rails 的新手,上个月左右才开始学习。我有一个关于建立模特协会的问题。我已经遇到了一个问题,我认为我需要回去对我已经做过的事情做出改变。这是场景。

我正在为乐队创建一个应用程序来列出即将举行的活动。

有几个重要因素 有

  • 多个乐队列出活动
  • 每个乐队都会有独特的活动,但也会与其他一些乐队共享一些活动(即,当使用该系统的两个乐队在同一场地一起演奏时)
  • 有关于的某些信息每个活动对于活动中的所有乐队来说都是通用的(即场地、日期、时间、城市、州等)。 每个
  • 活动都有特定于某个乐队的某些信息(即特定的 VIP 门票购买 URL、他们自己的描述)事件等)

目前这是它的设置方式:

class Event < ActiveRecord::Base    
  belongs_to :venue
  scope :upcoming, where('date >= ?', Date.today)
end  

class Venue < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

我还没有真正对艺术家模型做太多事情,因为我只是专注于将事件输入到系统中,并为硬编码的 1 位艺术家正确列出。

基本上现在它的工作原理是用户去创建一个事件表格中,他们选择日期,下一行是场地名称。场地名称表格会使用数据库中的场地名称 + 城市自动完成。如果用户在系统中已有的场地创建活动,那么它本质上是通过场地模型将活动信息保存为嵌套属性。如果该地点不在系统中,则会出现位置数据的附加输入字段,然后创建一个新地点并将其与该事件关联。

当我开始考虑让艺术家共享事件并维护仅针对该艺术家的事件的特定细节的最佳方式时,事情开始变得有点棘手。我可以将每个事件视为一个单独的事件,这很容易,但是除了将这些名字直接输入到 1 字段的艺术家之外,我不确定如何才能说这位艺术家在这一天与这位艺术家一起表演。然后我也失去了 ActiveRecords 的很多魔力,比如哪些艺术家与哪些其他艺术家一起表演,等等。

到目前为止,事件只有 1 个模型,但我想也许最好保留 1 个中心模型仅适用于多个乐队未共享的事件基础知识的模型,例如场地、城市、州等,然后将它们链接到辅助 event_details 模型以获取乐队特定信息。

您是否会建议使用多个模型,或者是否有更好的方法让我将演奏多个活动的乐队链接在一起,而不是创建一个链接回多个艺术家共享的 1 个主要活动的活动详细信息模型?我是否应该在事件表上创建某种类型的唯一事件标识符,以便将具有不同 ID 的事件视为另一个乐队正在演奏的同一事件?我看到的唯一问题是,我将加倍处理数据库中的大量数据……我本质上希望能够在查看事件数据时说,艺术家正在演奏的这一天可以这场演出,在这个场地,与这些其他乐队一起演出,而不必简​​单地制作一个“other_artists”栏,乐队只需在其中列出与他们一起表演的艺术家。

抱歉,我希望这是有道理的...提前感谢您的任何建议!

I am new to Rails just been learning the last month or so. I have a question regarding setting up model associations. I have sort of hit a spot where I think I need to go back and make changes to what I've already done. Here is the scenario.

I am creating an application for bands to list upcoming events.

There are a few important factors

  • There are multiple bands listing events
  • Each band will have unique events but will also share some events with some of the other bands (i.e. when two bands using this system are playing the same venue together)
  • There is certain information about each event which will be common to all bands on the event (i.e. venue, date, time, city, state, etc)
  • There is certain information about each event that is specific to one band (i.e. specific VIP ticket purchase URLs, their own description of the event, etc)

Currently this is how its set up:

class Event < ActiveRecord::Base    
  belongs_to :venue
  scope :upcoming, where('date >= ?', Date.today)
end  

class Venue < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

I haven't really done much with the artist model yet as I was just focusing on getting the events in to the system and listed properly for 1 artist which was hardcoded in.

Essentially right now how it works is the user goes to the create an event form, they select the date and the next line is venue name. The venue name form autocompletes with the Venue Name + City from the DB. If the user is creating a event at a venue already in the system it essentially saves the Event info via the Venue model as a nested attribute. If the venue is not in the system additional input fields for location data appear and then a new venue is created and associated with that event.

It begins to get a bit tricky when I start to think about the best way to have artists share events and maintain particular details about events that are specific to that artist only. I could just treat every event as an individual event and it would be easy but then I am not sure how I would be able to say this artist is performing with this artists on this date aside from the artists inputting those names directly into 1 field. I also then lose a lot of the magic of the ActiveRecords with respect to which artists performed with which other artists where, etc.

As of now there is just 1 model for events, but i am thinking maybe it might be best to keep 1 central model for just the event basics that aren't shared by multiple bands like venue, city, state, etc and then link those to a secondary event_details model for band specific information.

Would you suggest the multiple models or is there a better way for me to link bands playing multiple events together than creating an event detail model linking back to the 1 main event that multiple artists share? Should I just create some type of unique event identifier on the events table so that I am treating an event with a different ID as the same event another band is playing? Only issue I see with that is then I am going to be doubling up on a lot of that data that is in my database...I essentially want to be able to while looking at event data say ok on this date the artist is playing this show, at this venue, with these other bands with out having to just simple make an "other_artists" column where the bands would simply list artists they are performing with.

sorry i hope that makes sense...thanks in advance for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2025-01-14 11:50:39

我认为你真正的问题是你需要处理艺术家和活动之间的多对多关系。活动将有许多艺术家预订,艺术家的日程上也会有许多活动。

我会创建一个“预订”类,专门将一位艺术家与一项活动联系起来。此预订将是满足该艺术家在该活动中演奏的任何特殊要求或特殊安排的好地方。使用 ActiveRecord 的“has_many :through”关系将使这一过程变得非常简单,并为您如何查询数据提供很大的灵活性。以下是我的做法:

class Event < ActiveRecord::Base    
  belongs_to :venue
  has_many :artists, :through => :bookings
  scope :upcoming, where('date >= ?', Date.today)
end  

class Venue < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

class Artist < ActiveRecord::Base
  has_many :events, :through => :bookings
end

class Booking < ActiveRecord::Base
  belongs_to :artist
  belongs_to :event
  attr_accessible :special_requirements, :special_arrangements
end

通过这些关系查询数据时获得的灵活性的一些示例:

 @venue.events
 @venue.events.where(:id => specific_event.id).first.artists
 @event.artists
 @artist.events
 @event.bookings each do |b|
   b.artist
   b.special_requirements
 end

希望这会有所帮助。

I think your real issue here is that you are going to need to handle a many-to-many relationship between artists and events. Events will have many artists booked, and artists will have many events on their schedule.

I would create a "Booking" class that specifically ties one artist to one event. This booking will be a good place to hold any special requirements or special arrangements for that artist playing at that event. Using ActiveRecord's "has_many :through" relationship will make this very straight forward and give you a lot of flexibility in how you can query the data. Here is how I would do it:

class Event < ActiveRecord::Base    
  belongs_to :venue
  has_many :artists, :through => :bookings
  scope :upcoming, where('date >= ?', Date.today)
end  

class Venue < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

class Artist < ActiveRecord::Base
  has_many :events, :through => :bookings
end

class Booking < ActiveRecord::Base
  belongs_to :artist
  belongs_to :event
  attr_accessible :special_requirements, :special_arrangements
end

Some examples the flexibility you get in querying data via these relationships:

 @venue.events
 @venue.events.where(:id => specific_event.id).first.artists
 @event.artists
 @artist.events
 @event.bookings each do |b|
   b.artist
   b.special_requirements
 end

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文