在我的Rails 4应用程序中,我有以下模型:
User
has_many :administrations
has_many :calendars, through: :administrations
has_many :comments
has_many :calendar_comments, through: :calendars, :source => :comments
Calendar
has_many :administrations
has_many :users, through: :administrations
has_many :posts
has_many :comments, through: posts
has_many :ads
Administration
belongs_to :user
belongs_to :calendar
Post
belongs_to :calendar
has_many :comments, as: :commentable
Ad
belongs_to :calendar
has_many :comments, as: :commentable
Comment
belongs_to :commentable, polymorphic: true
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
我需要访问comments的是belong_to一个ad从calendar的ad belongs_to.
这就是我在Calendars#Index行动中要做的事情:
@posts_comments = @user.calendar_comments.where(commentable_type: "Post").order("created_at DESC").limit(5)
@ads_comments …Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails has-many-through polymorphic-associations ruby-on-rails-4
我之前使用过has_and_belongs_to_many,并已转换为has_many:through.以下是它如何查找可以让很多用户玩的游戏列表.有了这个,我可以做game.users和user.games ....:
class Game < ActiveRecord::Base
has_many :game_users, :dependent => :destroy
has_many :users, :through => :game_users, :uniq => true
end
class User < ActiveRecord::Base
has_many :game_users, :dependent => :destroy
has_many :games, :through => :game_users, :uniq => true
end
class GameUser < ActiveRecord::Base
belongs_to :game
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
我的连接表的数据库迁移:
create_table :game_users, :id => false do |t|
t.column :game_id, :integer
t.column :user_id, :integer
t.column :player_index, :integer
end
Run Code Online (Sandbox Code Playgroud)
我不太确定我得到了这一切,请帮我查一下我的事实:
依赖=>:destroy是否正确?如果游戏或用户被销毁,我希望删除'game_users'连接表条目 - 但我不希望删除用户如果删除游戏,反之亦然......?
uniq字段应该表示游戏仅包含唯一用户,并且用户仅包含唯一游戏.那是对的吗?
像以前一样进行数据库迁移:id => false.这仍然是正确的做法吗?我试图在控制台中摧毁一个游戏,并对丢失的内容进行投诉......所以我猜不到并试图理解为什么.
我发现rails活动记录关联非常混乱.我想他们不应该是!
这是我的模特:
class Tag < ActiveRecord::Base
# id, name
has_many :taggings
end
class Tagging < ActiveRecord::Base
# id, tag_id, owner_id, target_type, target_id
belongs_to :tag
belongs_to :owner, :class_name => 'User'
belongs_to :target, :polymorphic => true
validates_uniqueness_of :tag_id, :scope => [ :target_id, :target_type, :owner_id ]
end
class Asset < ActiveRecord::Base
# id, owner_id, title, type, etc
belongs_to :owner, :class_name => 'User'
has_many :taggings, :as => :target
has_many :taggers, :through => :taggings, :source => :owner, :uniq => true
has_many :tags, :through => :taggings, :uniq …Run Code Online (Sandbox Code Playgroud) 我不确定我的问题是什么,所以这个问题可能需要更多的澄清,但这里似乎是最相关的:
我有一个has_many :through和连接模型有一些不是外键的字段.当我构建模型并尝试保存时,我从连接模型的非外键字段获得验证错误.
我的文件看起来像:
Person.rb
has_many :wedding_assignments, :dependent => :destroy
has_many :weddings, :through=>:wedding_assignments
accepts_nested_attributes_for :weddings
accepts_nested_attributes_for :wedding_assignments
Wedding.rb
has_many :wedding_assignments, :dependent => :destroy
has_many :people, :through=>:wedding_assignments
accepts_nested_attributes_for :people
accepts_nested_attributes_for :wedding_assignments
WeddingAssignment.rb
belongs_to :person
belongs_to :wedding
validates_presence_of :role, :person, :wedding
Run Code Online (Sandbox Code Playgroud)
(角色是一个字符串)
people_controller.rb
def new
@person = Person.new
1.times do
wedding = @person.weddings.build
1.times do
assignment = wedding.wedding_assignments.build
assignment.person = @person
assignment.wedding = wedding
end
end
end
def create
@person = Person.new(params[:person])
@person.weddings.each do |wedding|
wedding.wedding_assignments.each do |assignment|
assignment.person = …Run Code Online (Sandbox Code Playgroud) 我有一个如下模型设置:
class User
has_many :items
has_many :words, :through => :items
end
class Item
belongs_to :user
belongs_to :word
default_scope where(:active => true)
end
class Words
has_many :items
end
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是default_scope没有应用于以下关联:
user.words
Run Code Online (Sandbox Code Playgroud)
而不是这个SQL:
SELECT `words`.* FROM `words` INNER JOIN `items` ON `words`.id = `items`.word_id WHERE ((`items`.user_id = 1)) AND ((`items.active = 1))
Run Code Online (Sandbox Code Playgroud)
我在日志中得到这个SQL:
SELECT `words`.* FROM `words` INNER JOIN `items` ON `words`.id = `items`.word_id WHERE ((`items`.user_id = 1))
Run Code Online (Sandbox Code Playgroud)
我认为这是因为默认范围适用于常规模型及其子关联,但不适用于连接表.
使连接表范围在关联之间工作而不需要指定它的正确Rails方法是什么?有人存在吗?
scope ruby-on-rails has-and-belongs-to-many has-many-through default-scope
我有一个符合以下模式的模型:
class foo < ActiveRecord::Base
has_many :bar, :dependent => :destroy
has_many :baz, :through => :bar, :uniq => true,
:after_add => :update_baz_count,
:after_remove => :update_baz_count
def update_baz_count(record)
debugger
# stuff...
end
end
Run Code Online (Sandbox Code Playgroud)
我试图通过bar保持与foo相关的唯一baz的数量.但由于某种原因,当我向foo添加一个bar(必须有一个baz)时,永远不会调用after_add和after_remove回调.有什么想法吗?我已将这些回调与habtm一起使用,并且它们工作正常.
谢谢.
似乎ActiveRecord的counter_cache功能可能导致计数器缓存增加两次.我看到这种行为的场景是当我有两个模型has_many :through通过连接模型彼此建立关系时(即:Teacher有很多Student通过Classroom).使用has_many :through生成的方法直接关联教师和学生(无需手动创建连接记录)时,计数会增加2倍.示例:teacher.students << Student.create(name: "Bobby Joe")原因teacher.students_count增加2.
请帮助我找到一个解决方案,减轻或消除这个问题,同时允许我继续使用内置的计数器缓存和通过has_many :through关系的质量分配.
我花了很多时间寻找解决方案并将问题提取到一个小的测试应用程序,这是我可以创建的最简单的失败示例.任何有助于我解决这个问题的额外细节都应该在下面.
示例模式和模型:
create_table :teachers do |t|
t.string :name
t.integer :students_count, default: 0
t.timestamps
end
class Teacher < ActiveRecord::Base
has_many :classrooms
has_many :students, :through => :classrooms
end
create_table :students do |t|
t.string :name
t.integer :teachers_count, default: 0
t.timestamps
end
class Student < ActiveRecord::Base
has_many :classrooms
has_many :teachers, :through => :classrooms
end
create_table :classrooms do |t|
t.references …Run Code Online (Sandbox Code Playgroud) 我正在使用rails创建新产品,并希望为每个产品添加一个类别.
我有三个表:产品,类别和分类(存储产品和类别之间的关系).我正在尝试使用嵌套属性来管理分类的创建,但不确定应如何更新我的控制器和视图/表单,以便新产品也更新分类表.
这是我的模特:
class Product < ActiveRecord::Base
belongs_to :users
has_many :categorizations
has_many :categories, :through => :categorizations
has_attached_file :photo
accepts_nested_attributes_for :categorizations, allow_destroy: true
attr_accessible :description, :name, :price, :photo
validates :user_id, presence: true
end
class Category < ActiveRecord::Base
attr_accessible :description, :name, :parent_id
acts_as_tree
has_many :categorizations, dependent: :destroy
has_many :products, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :product
attr_accessible :category_id, :created_at, :position, :product_id
end
Run Code Online (Sandbox Code Playgroud)
这是我的新产品控制器:
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.json { render …Run Code Online (Sandbox Code Playgroud) 我的架构中有Articles和Journals可与被标记Tags.这需要has_many through:与我的Tagging连接表的多态关系关联.
好的,这是一个简单且记录良好的部分.
我的问题是Articles可以同时拥有主标签和子标签.主要标签是我最感兴趣的,但我的模型也需要跟踪这些子标签.子标签只是描述Article不太重要的标签,但来自同一个全局池Tags.(事实上,一个Article人的主要标签可能是另一个人的子标签).
实现这一点需要Article模型与模型有两个关联,Tagging并且两个has_many through:关联Tags(即#tagssub-tags)
这是我到目前为止所做的,虽然有效但不保持主标签和子标签分开.
class Article < ActiveRecord::Base
has_many :taggings, as: :taggable
has_many :tags, through: :taggings
has_many :sub_taggings, as: :taggable, class_name: 'Tagging',
source_type: 'article_sub'
has_many :sub_tags, through: :sub_taggings, class_name: 'Tag', source: :tag
end
class Tagging < ActiveRecord::Base
# id :integer
# taggable_id :integer
# taggable_type :string(255)
# tag_id :integer
belongs_to :tag …Run Code Online (Sandbox Code Playgroud) tagging activerecord ruby-on-rails has-many-through polymorphic-associations
当在中间的关系上实现多个外键时,我很难从分层父关系中获取所涉及的游戏列表.
鉴于League Object NFC,找到它的所有Game对象[G1,G3,G4]
# id :integer not null, primary key
# name :string
class League
has_many :teams
# has_many :games, :through => :teams (Is there some way to do this?)
end
# id :integer not null, primary key
# team_name :string
# league_id :integer
class Team
belongs_to :league
has_many :home_games, :foreign_key => team_a_id, :source => :game
has_many :away_games, :foreign_key => team_b_id, :source => :game
end
# id :integer not null, primary key
# game_name :string
# team_a_id …Run Code Online (Sandbox Code Playgroud) has-many-through ×10
ruby-on-rails ×10
activerecord ×6
ruby ×2
callback ×1
model ×1
scope ×1
tagging ×1