Ruby on rails RubyonRails——使用multiple对同一个模型有很多关联
我有一个简单的程序,其中一个事件可以由用户创建,事件类知道是谁创建的:Ruby on rails RubyonRails——使用multiple对同一个模型有很多关联,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个简单的程序,其中一个事件可以由用户创建,事件类知道是谁创建的: class Event < ActiveRecord::Base # the owner of the event belongs_to :user end class User < ActiveRecord::Base # a user has 0 to n created events has_many :events end 如果我把关联关系搞错了,如果我想得太多了,还有一个更简
class Event < ActiveRecord::Base
# the owner of the event
belongs_to :user
end
class User < ActiveRecord::Base
# a user has 0 to n created events
has_many :events
end
如果我把关联关系搞错了,如果我想得太多了,还有一个更简单的方法,我会非常感激任何我能得到的关于我错在哪里的建议。非常感谢。如果您希望
用户能够“保存”事件(从仪表板而不是数据库的意义上来说),为什么不在事件中添加保存_以备以后使用
标志呢
rails g迁移将_save _for _later添加到_eventssave _for _later:boolean
然后在事件
模型中,可以添加以下范围:
scope :flagged_events_for, (lambda do |user|
where('user_id = ? AND save_for_later = ?', user.id, true)
end)
然后,如果您希望用户
能够“保存”事件(从仪表板而不是数据库的角度来看),您可以使用Event.flagged_events_for(user)
来“询问”应用程序中其他地方的事件
模型的适当实例,为什么不在事件
中添加一个保存_以备以后使用
标志
rails g迁移将_save _for _later添加到_eventssave _for _later:boolean
然后在事件
模型中,可以添加以下范围:
scope :flagged_events_for, (lambda do |user|
where('user_id = ? AND save_for_later = ?', user.id, true)
end)
然后,如果您希望用户
能够“保存”事件(从仪表板而不是数据库的角度来看),您可以使用Event.flagged_events_for(user)
来“询问”应用程序中其他地方的事件
模型的适当实例,为什么不在事件
中添加一个保存_以备以后使用
标志
rails g迁移将_save _for _later添加到_eventssave _for _later:boolean
然后在事件
模型中,可以添加以下范围:
scope :flagged_events_for, (lambda do |user|
where('user_id = ? AND save_for_later = ?', user.id, true)
end)
然后,如果您希望用户
能够“保存”事件(从仪表板而不是数据库的角度来看),您可以使用Event.flagged_events_for(user)
来“询问”应用程序中其他地方的事件
模型的适当实例,为什么不在事件
中添加一个保存_以备以后使用
标志
rails g迁移将_save _for _later添加到_eventssave _for _later:boolean
然后在事件
模型中,可以添加以下范围:
scope :flagged_events_for, (lambda do |user|
where('user_id = ? AND save_for_later = ?', user.id, true)
end)
然后,您就可以使用事件“询问”应用程序中其他位置的事件的相应实例。为(用户)标记的事件将用户作为用户保存的\u id添加到事件中的问题是,只有一个用户能够保存该事件。如果我理解正确,您希望用户能够保存事件(他们可能创建了,也可能没有创建?)以显示在其仪表板中。在这种情况下,您将需要类似于has\u和\u归属\u many
或has\u many to
类型关系的内容,因此需要另一个表。也许像使用手机这样的事情已经发生了很多
class SavedEvent < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
class User < ActiveRecord::Base
# a user has 0 to n created events
has_many :events
has_many :saved_events
# a user has 0 to n saved events through the favorites table
has_many :later_events, through: :saved_events
end
然后创建一个新的事件
def create
#wrap in transaction
Event.transaction do
#build the event through the association
@event = current_user.events.create!(event_params)
#save it to the current users saved events
current_user.saved_events.create!(event: @event)
end
end
交易和交易!方法只需使其在无法创建事件并添加到已保存事件时回滚,而不会在创建时以不在已保存/以后的事件表中的事件结束
当另一个用户想要保存事件时,在您的控制器中,您只需要为他们执行当前用户.saved\u事件.create(event:event)
,它就会保存它
然后在仪表板中,您可以执行当前用户。以后的\u事件
,并获取他们保存的所有事件的集合 将用户作为user\u saved\u id
添加到事件中的问题是,只有一个用户能够保存该事件。如果我理解正确,您希望用户能够保存事件(他们可能创建了,也可能没有创建?)以显示在其仪表板中。在这种情况下,您将需要类似于has\u和\u归属\u many
或has\u many to
类型关系的内容,因此需要另一个表。也许像使用手机这样的事情已经发生了很多
class SavedEvent < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
class User < ActiveRecord::Base
# a user has 0 to n created events
has_many :events
has_many :saved_events
# a user has 0 to n saved events through the favorites table
has_many :later_events, through: :saved_events
end
然后创建一个新的事件
def create
#wrap in transaction
Event.transaction do
#build the event through the association
@event = current_user.events.create!(event_params)
#save it to the current users saved events
current_user.saved_events.create!(event: @event)
end
end
交易和交易!方法只需使其在无法创建事件并添加到已保存事件时回滚,而不会在创建时以不在已保存/以后的事件表中的事件结束
当另一个用户想要保存事件时,在您的控制器中,您只需要为他们执行当前用户.saved\u事件.create(event:event)
,它就会保存它
然后在仪表板中,您可以执行当前用户。以后的\u事件
,并获取他们保存的所有事件的集合 将用户作为user\u saved\u id
添加到事件中的问题是,只有一个用户能够保存该事件。如果我理解正确,您希望用户能够保存事件(他们可能创建了,也可能没有创建?)以显示在其仪表板中。在这种情况下,您将需要类似于has\u和\u归属\u many
或has\u many to
类型关系的内容,因此需要另一个表。也许像使用手机这样的事情已经发生了很多
class SavedEvent < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
class User < ActiveRecord::Base
# a user has 0 to n created events
has_many :events
has_many :saved_events
# a user has 0 to n saved events through the favorites table
has_many :later_events, through: :saved_events
end
然后创建一个新的事件
def create
#wrap in transaction
Event.transaction do
#build the event through the association
@event = current_user.events.create!(event_params)
#save it to the current users saved events
current_user.saved_events.create!(event: @event)
end
end
交易和交易!方法只需使其在无法创建事件并添加到已保存事件时回滚,而不会在创建时以不在已保存/以后的事件表中的事件结束
当另一个用户想要保存事件时,在您的控制器中,您只需要为他们执行当前用户.saved\u事件.create(event:event)
,它就会保存它
然后在仪表板中,您可以执行当前用户。以后的\u事件
,并获取他们保存的所有事件的集合 将用户作为user\u saved\u id
添加到事件中的问题是,只有一个用户是abl