开发者

ROR: has many relationships help

开发者 https://www.devze.com 2023-03-28 06:53 出处:网络
I am new to rails and i want to get some help regarding has many relationships. 开发者_运维知识库

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?


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 or has_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.


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.


In your userstable you have to create an new column event_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.

class User < ActiveRecord::Base
  has_many :events
end

class Event < ActiveRecord::Base
  belongs_to :user
end

Doing this, in your events table you should add a column user_id

0

精彩评论

暂无评论...
验证码 换一张
取 消