ROR:有很多关系帮助
我是 Rails 新手,我想获得一些有关许多关系的帮助。
我将 https://github.com/bokmann/rails3_fullcalendar 中的完整日历应用程序包含到我的新应用程序中。
我有一个用户模型。我希望所有用户每人都有一个日历。现在,所有用户的日历都是相同的。
我尝试过:
在 user.rb
...
belongs_to :events
在 events.rb
has_many :users
这不起作用。
我通过创建 calendar.rb 做了同样的事情,但它仍然不起作用
有什么想法吗?
I am new to rails and i want to get some help regarding has many relationships.
I included the full calendar app from https://github.com/bokmann/rails3_fullcalendar into my new application.
I have a user model. I want all the users to have a one calendar each. Right now the calendar is same for all users.
I tried:
in user.rb
...
belongs_to :events
in events.rb
has_many :users
This did not work.
I did the same by creating calendar.rb but it still did not work
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你应该阅读 Rails 指南: http://guides.rubyonrails.org/association_basics.html
更仔细一点。
我想您可能想要的是事件和用户实例使用
has_and_belongs_to_many
或has_many :through
相互连接。之后,实现用户参与许多事件或的业务逻辑一个活动有很多用户(参加者)很容易 - 我想这就是你想要的,因为你的帖子有点不清楚。日历只是显示特定用户事件的好方法。
我希望这有帮助。
I think you should read Rails Guides: http://guides.rubyonrails.org/association_basics.html
a little bit more carefully.
I guess what you probably want is Event and User instances to connect with each other using
has_and_belongs_to_many
orhas_many :through
After that, implementing a business logic where user attends to many events or an event has many users (attenders) is easy - I guess that is what you want, because you're bit unclear in your post. And a calendar is just a nice way to display events for a particular user.
I hope this helps.
我不确定日历插件是如何工作的,但是为了获取仅属于用户的事件,您需要利用关联。如果您使用的是常见的身份验证 gem 之一,您可以执行 current_user.events 来获取属于该用户的所有事件。
I'm not sure how the calendar plugin works, but for getting events that only belongs to a user you need to take advantage of the associations. If you are using one of the common authentication gems you could do current_user.events to get all events that belongs_to that user.
在您的
users
表中,您必须创建一个新列event_id
,以便您可以毫无问题地进行此关联。但我想它应该是相反的顺序,一个用户应该有一个或多个事件,一个事件应该属于一个用户。
为此,您应该在事件表中添加一列
user_id
In your
users
table you have to create an new columnevent_id
so you can do this association with no problem.but I guess it should be in the invert order, a User should have one or more events and a event should belong to a User.
Doing this, in your events table you should add a column
user_id