Ruby on rails Activerecord多对多关系不起作用
我正在做一个推特克隆人,我的一些联想一直有问题。我希望一个用户有很多tweet。我的tweet将与hashtags有多对多的关系,因此理论上我希望能够调用User.find(1).hashtags并拥有用户的hashtags列表(编辑:解决了这个问题)。我也很难将标签推到特定的推文中 以下是我的模型:Ruby on rails Activerecord多对多关系不起作用,ruby-on-rails,activerecord,rails-activerecord,Ruby On Rails,Activerecord,Rails Activerecord,我正在做一个推特克隆人,我的一些联想一直有问题。我希望一个用户有很多tweet。我的tweet将与hashtags有多对多的关系,因此理论上我希望能够调用User.find(1).hashtags并拥有用户的hashtags列表(编辑:解决了这个问题)。我也很难将标签推到特定的推文中 以下是我的模型: class User < ActiveRecord::Base has_many :tweets end class Tweet < ActiveRecord::Base b
class User < ActiveRecord::Base
has_many :tweets
end
class Tweet < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :hashtags
end
class Hashtag < ActiveRecord::Base
has_and_belongs_to_many :tweets
end
class用户
以下是我的迁移:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :password
t.timestamps
end
end
end
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.string :content
t.belongs_to :user
t.timestamps
end
end
end
class CreateHashtags < ActiveRecord::Migration
def change
create_table :hashtags do |t|
t.string :tag
t.timestamps
end
end
end
class CreateHashtagsTweets < ActiveRecord::Migration
def change
create_table :hashtags_tweets, id: false do |t|
t.references :hashtag
t.references :tweet
end
end
end
class CreateUsers
任何帮助都将不胜感激!谢谢 添加一个
has\u many:hashtags,->{uniq},通过::tweets
关联到用户模型awesome,这很有效,谢谢!