简体   繁体   中英

How to use verbs (to map graph relationships) for ruby on rails models instead of nouns

Usually, Ruby on Rails models are named using nouns like User , Photo , ..., and in a graph, those names are usually good for nodes. What should I use for relationships like "is tagged in", "tags"? What's the best way to map them?

I am sorry if I am misunderstanding your question, but, I think in general relationships are nouns. For example if by a “tag” you mean a person being mentioned in a social media post, then the tag is a noun, it is a link between the person and the post. In rails this would be represented by three models.

Person Post Tag

where

class Person <  ActiveRecord::Base
  has_many :tags
  …
  def is_tagged_in
    # find the posts the user is “tagged” in
  end    
end

class Post <  ActiveRecord::Base
  has_many :tags
  …
end

class Tag <  ActiveRecord::Base
  belongs_to :post
  belongs_to :person
  …
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM