Tag: 协会

rails(3.2)中的关联和(多个)外键:如何在模型中描述它们,并编写迁移

我有3个模型:问题,选项,规则 问题has_many选项; 选项需要question_id的外键 规则表由3个foreign_keys组成: 2列/对question_ids的引用 – >名为’assume_question_id’和’consequent_question_id’的外键 1列/对option_id的引用 – >名为option_id或condition_id的外键 规则关联:问题有多个规则; 和Option has_one规则 我想了解如何为此编写迁移,以及如何与我在模型中编写的’has_many’/’belongs_to’语句以及我可以包含在模型中的’:foreign_key’选项相关联。 我有这个用于我的Option迁移,但我不确定“add_index”语句如何在外键方面起作用,以及如何将它用于我的规则迁移:(我的问题和选项模型有适当的has_many和belongs_to语句 – 并且工作正常) class CreateOptions < ActiveRecord::Migration def change create_table :options do |t| t.integer :question_id t.string :name t.integer :order t.timestamps end add_index :options, :question_id end end 感谢您的帮助!

在轨道上的ruby上构建方法

对rails很新,我正在跟踪使用rails 3.1在Agile Web开发中找到的Depot项目。 一切都很好,直到我迷失了本书使用“构建”方法。 @cart = current_cart product = Product.find(params[:product_id]) @line_item = @cart.line_items.build(product: product) 我的谷歌搜索让我明白.build方法只是一种在表格中创建行的更简洁的方法(表格之间有关联)。 但是在上面的代码中,我期待代码看起来像这样: @line_item = @cart.line_items.build(product_id => params[:product_id]) 我不明白为什么作者必须存储整行产品(product = Product.find(params [:product_id]))而不是仅仅获取product_id … 还有比我能理解的更多的东西吗?

需要来自rails join table的数据,has_many:through

我有3个表 – 用户,东西和以下。 用户可以通过以下表格进行操作,将user_id与things_id相关联。 这意味着: class User has_many :things, :through => :follows end class Thing has_many :users, :through => :follows end class Follow belongs_to :users belongs_to :things end 所以我可以毫无问题地检索thing.users。 我的问题是如果在下面的表中,我有一个名为“relation”的列,所以我可以将一个关注者设置为“admin”,我希望能够访问该关系。 所以在循环中我可以做类似的事情: 有没有办法将关系包含在原始用户对象中? 我试过:select => “follows.relation” ,但它似乎没有加入属性。

如何初始化rails gem,它将关联添加到用户指定的模型

我正在创建一个gem,它需要为用户定义的某些模型添加关联。 我有一个初始化文件,可以通过rails生成器命令将其复制到应用程序中,这是用户将指定要添加关联的模型的位置。 BloggyGem.setup do |config| config.user = User config.post = Post end gem内部,我有这个指定 opts = BloggyGem.settings opts.user.has_many opts.post.to_s.downcase.pluralize.to_sym, :class_name => opts.post.model_name opts.post.belongs_to opts.user.to_s.downcase.singularize.to_sym, :class_name => opts.user.model_name 我的测试是在传递我的gem,但导轨初始化略有不同,所以想要确保最好的方法。

如何引用我的友谊表以有条件地显示代码?

在跟随Ryan Bates的railscast之后,我已经成功地在我的Ruby on Rails应用程序中为用户建立了友谊自我参与协会。 我可以成功地关注,取消关注,并查看谁在关注。 现在我想引入另一个元素,我不知道该怎么做。 在我的current_user仪表板页面上,我想根据current_user是否跟随任何人以及是否有任何人跟随current_user来有条件地显示元素。 换句话说,如果current_user没有跟随任何人,我不想显示以下内容: Following :delete, :class => “unfollow” %> 如果没有任何人关注current_user,我不想显示: Followers 这些是如何建立关联: Friendship.rb belongs_to :user belongs_to :friend, :class_name => “User” User.rb has_many :friendships has_many :friends, :through => :friendships has_many :inverse_friendships, :class_name => “Friendship”, :foreign_key => “friend_id” has_many :inverse_friends, :through => :inverse_friendships, :source => :user def friends_workouts @friends_workouts ||= Workout.find_all_by_user_id(self.friends.map(&:id), :order […]

如何限制视图

在跟随Ryan Bates的railscast之后,我已经成功地在我的Ruby on Rails应用程序中为用户建立了友谊自我参与协会。 我可以成功地关注,取消关注,并查看谁在关注。 现在我想引入另一个元素,我不知道该怎么做。 当访问用户的用户/显示页面时, current_user已经是friends …用于检查现有友谊的Friendships表的语法是什么。 以下是如何建立关联。 Friendship.rb belongs_to :user belongs_to :friend, :class_name => “User” User.rb has_many :friendships has_many :friends, :through => :friendships has_many :inverse_friendships, :class_name => “Friendship”, :foreign_key => “friend_id” has_many :inverse_friends, :through => :inverse_friendships, :source => :user def friends_workouts @friends_workouts ||= Workout.find_all_by_user_id(self.friends.map(&:id), :order => “created_at DESC”, :limit => 3) end

Rails用户对电影的评论

我有三个脚手架用户 , 评论和Movies 在我的应用程序中,我希望用户 评论 Movies ,不同的用户可以评论 Movie页面。 如何创建关联代码,让用户在电影上添加评论,然后在电影页面上显示所有评论? 请你告诉我计算评论的代码,所以显示有多少评论并以整数显示 到目前为止我得到了什么 带有主题和正文电影表用户表的注释表 user.rb has_many: comments movies.rb has_many: comments comments.rb belongs_to :users belongs_to :movies 谢谢 !

如何根据多对多关系选择用户子集?

User和Organization通过Relationship具有多对多关联。 Relationship模型中的一个变量是一个名为member的布尔值。 user可以多次出现在Relationship模型中,因为user可以与多个organizations相关联。 如何从1)与任何organization没有关系的所有users中选择随机实例(因此不要出现在Relationship模型中)加上2)与组织有关系但与谁有关系的users member = false表示他们与组织的所有关系? 我想的是: user = User.where( relationship_id = nil || ??? ).offset(rand(0..100)).first

Rails validate_association与模型的错误消息

我使用模型中的validates_associated来使用其他模型的validation代码。 这个问题是validation失败的消息是“..无效”。 我想将模型validation失败的实际描述性错误冒出来! 我发现了这个问题: 与模型的错误消息相关的validation 这看起来像一个非常接近的解决方案: module ActiveRecord module Validations class AssociatedBubblingValidator value)) end end end end end module ClassMethods def validates_associated_bubbling(*attr_names) validates_with AssociatedBubblingValidator, _merge_attributes(attr_names) end end end end 但实际上它遇到了一个错误: undefined method `valid?’ for # 任何人都可以帮助完成这个几乎工作的工作!? 完整的错误跟踪是: undefined method `valid?’ for # Extracted source (around line #6): 4 5 6 7 8 9 def validate_each(record, […]

Rails – 测试夹具错误NoMethodError:nil的未定义方法`type’:NilClass

我在运行使用具有模型之间关联的夹具的测试时遇到问题。 一旦我运行rake test ,这就是我得到的错误: ERROR[“test_truth”, SevenPortfolioTest, 0.005154775] test_truth#SevenPortfolioTest (0.01s) NoMethodError: NoMethodError: undefined method `type’ for nil:NilClass ERROR[“test_should_destroy_item_video”, SevenPortfolio::ItemVideosControllerTest, 0.008887804] test_should_destroy_item_video#SevenPortfolio::ItemVideosControllerTest (0.01s) NoMethodError: NoMethodError: undefined method `type’ for nil:NilClass ERROR[“test_should_get_index”, SevenPortfolio::ItemVideosControllerTest, 0.011364416] test_should_get_index#SevenPortfolio::ItemVideosControllerTest (0.01s) NoMethodError: NoMethodError: undefined method `type’ for nil:NilClass ERROR[“test_should_create_item_video”, SevenPortfolio::ItemVideosControllerTest, 0.014584266] test_should_create_item_video#SevenPortfolio::ItemVideosControllerTest (0.01s) NoMethodError: NoMethodError: undefined method `type’ for nil:NilClass ERROR[“test_should_get_new”, SevenPortfolio::ItemVideosControllerTest, 0.017282812] […]