views:

62

answers:

3

I'm brand new to Rails, so bear with me.

I have 3 models: User, Section, and Tick.

Each section is created by a user. My guess with this association:

class Section < ActiveRecord::Base
  has_one :user
end

Next, each user can "tick" off a section -- only once. So for each tick, I have a section_id, user_id, and timestamps. Here's where I'm stuck. Does this call for a "has_one :through" association? If so, which direction? If not, then I'm way off.

Which association works here?

Thanks!

A: 

Try Following

class Tick < ActiveRecord::Base
  belongs_to :user
  belongs_to :section
  validates_uniqueness_of :user_id, :scope => :section_id #There is only one user per section
end

Ref This

Salil
+1  A: 
class User < AR::Base
  has_many :ticks
  has_many :sections, :through => :ticks
end

class Section < AR::Base
  has_many :ticks
  has_many :users, :through => :ticks
end

class Tick < AR::Base
  belongs_to :user
  belongs_to :section
  validates_uniqueness_of :user_id, :scope => :section_id
end

Now to find the sections a user ticked you do user.sections, or to find all users who ticked a certain section you do section.users

What you have here is a many-to-many relation (a user can have many sections, and a section can belong to many users), so this requires a join model to associate them. In this case, the Tick model is acting as the join model.

Faisal
Thanks. Since a user_id is required to create a section, do I need to create a separate association there? Minus the :ticks association? User has_many :sections, Section has_one :users ?
jmccartie
hmm, if you want a one-to-many relation (user has many section, section belongs to one user), then you should do it as User has_many :sections, Section belongs_to :user. I first understood that users and sections are many-to-many related. But if you want them to be one-to-many, check PlanetMasters' answer.
Faisal
+1  A: 

This should be right:

class User < AR::Base
  has_many :sections   #user can have many sections
  has_many :ticks, :through => :sections   #user has many ticks, but the left outer join goes through sections
end

class Section < AR::Base
  belongs_to :user  #each section belongs to 1 user
  has_many :ticks   #each section can have a tick, because of can use has_many
end

class Tick < AR::Base
  belongs_to :section  #a tick belongs to a section
  has_one :user, :through => :sections   #belongs_to :through does not exist, use has_one through
end
PlanetMaster
The way relations are created in your code reflects that the relation is between the user and the tick, and the section is what joins them together. Shouldn't it be that the user and the section are related, and the tick is what binds them together? At least, that's how I see it
Faisal
I'm not sure that "belongs to :user" is correct -- I would think that it would be "has_one :user"? ... maybe my explanation is bad. Please correct me if I'm wrong or need to re-explain. This looks like a differing example from Faisal's...trying to figure out which is correct. Thank you for your help!!!
jmccartie