Tag: 协会

FactoryGirl覆盖关联对象的属性

这可能很简单,但我无法在任何地方找到一个例子。 我有两个工厂: FactoryGirl.define do factory :profile do user title “director” bio “I am very good at things” linked_in “http://my.linkedin.profile.com” website “www.mysite.com” city “London” end end FactoryGirl.define do factory :user do |u| u.first_name {Faker::Name.first_name} u.last_name {Faker::Name.last_name} company ‘National Stock Exchange’ u.email {Faker::Internet.email} end end 我想要做的是在创建配置文件时覆盖一些用户属性: p = FactoryGirl.create(:profile, user: {email: “test@test.com”}) 或类似的东西,但我不能正确的语法。 错误: ActiveRecord::AssociationTypeMismatch: User(#70239688060520) expected, […]

Ruby on Rails – 设置评论function

我正在尝试在我的Ruby on Rails应用程序上设置一个function,让用户可以查看图片。 我已经按照本指南作为参考。 http://ruby.about.com/od/rubyonrails/ss/blogpart4_4.htm 根据我在其他Ruby on Rails项目上的经验,我认为Posts / Comments关系模型可以用于图片/评论关系。 我首先生成了一个脚手架。 rails g scaffold review name:string body:text picture:references 我希望每个图片页面都有一个单独的页面用于自己的评论。 由于我的评论控制器不需要索引页面,因此我从routes.rb文件中删除了该行 resources: reviews 我通过创建路线替换了它 match ‘https://stackoverflow.com/pictures/:id/reviews’, to: ‘reviews#show’, via: ‘get’ match ‘https://stackoverflow.com/pictures/:id/reviews/edit’, to: ‘reviews#edit’, via: ‘get’ match ‘https://stackoverflow.com/pictures/:id/reviews/new’, to: ‘reviews#new’, via: ‘get’ 在这里,我的路径涉及在图片中嵌套评论。 路线 favorite_picture_path PUT https://stackoverflow.com/pictures/:id/favorite(.:format) pictures#favorite pictures_path GET https://stackoverflow.com/pictures(.:format) pictures#index POST https://stackoverflow.com/pictures(.:format) pictures#create new_picture_path […]

Rails:has_many通过命名空间模型无法正确返回

我有3个型号。 Rom::Favorite , Rom::Card , User 。 我在User has_many rom_cards through rom_favorites创建User has_many rom_cards through rom_favorites时遇到问题 这是我的模型的相关部分 罗::卡 class Rom::Card < ActiveRecord::Base has_many :rom_favorites, class_name: "Rom::Favorite", foreign_key: "rom_card_id", dependent: :destroy self.table_name = "rom_cards" end 用户 class User < ActiveRecord::Base # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :role has_many […]

Ruby on rails:使用belongs_to关联创建模型条目

我正在尝试在我的数据库中为具有belongs_to关系的模型添加新条目。 我有两个模型,乔布斯和客户。 很容易找到关于如何在这两者之间建立关联的教程(使用has_many和belongs_to),但我似乎无法找到实际使用关联的任何示例。 在我的代码中,我正在尝试为第一个客户创建一个新工作。 作业模型有一个client_id的属性,我知道我可能只是手动填充属性,但必须有一些ruby约定来轻松实现这一点。 Job.create(:client_id => 1, :subject => “Test”, :description => “This is a test”) 我可以很容易地将它放在我的代码中,但我觉得ruby有更好的方法来做到这一点。 这是我的模型设置方式 class Job < ActiveRecord::Base attr_accessible :actual_time, :assigned_at, :client_id, :completed_at, :estimated_time, :location, :responded_at, :runner_id, :status, :subject, :description belongs_to :client end class Client < User has_many :jobs end class User < ActiveRecord::Base attr_accessible :name, :cell, :email, :pref end

优雅地从has_many中选择属性:通过Rails中的连接模型

我想知道在has_many中通过连接模型选择属性的最简单/最优雅的方法是:通过关联。 假设我们有Items,Catalogs和CatalogItems,它们包含以下Item类: class Item :catalog_items end 另外,假设CatalogueItems有一个position属性,并且在任何目录和任何项目之间只有一个CatalogueItem。 检索position属性最明显但又有点令人沮丧的方法是: @item = Item.find(4) @catalog = @item.catalogs.first @cat_item = @item.catalog_items.first(:conditions => {:catalog_id => @catalog.id}) position = @cat_item.position 这很烦人,因为我们应该能够做@ item.catalogs.first.position,因为我们已经完全指定了我们想要的位置:对应于@ item目录的第一个位置。 我发现得到这个的唯一方法是: class Item :catalog_items, :select => “catalogue_items.position, catalogs.*” end 现在我可以做Item.catalogs.first.position。 但是,这看起来有点像黑客 – 我在Catalog实例上添加了一个额外的属性。 它还开辟了在两种不同情况下尝试使用视图的可能性,其中我使用Catalog.find或@ item.catalogs填充@catalogs。 在一种情况下,位置将在那里,而在另一种情况下,它不会。 有人有一个很好的解决方案吗? 谢谢。

Mongomapper与Array有很多问题

我想列出用户表单中用户任务列表的所有任务名称 但是,当我使用下面的代码时,我收到以下消息: undefined method `task_id’ for … 这是我的课程: class User include MongoMapper::Document key :name, String key :tasklist, Array # I need this to hold ObjIds many :tasks, :in => :tasklist, :class_name => ‘Task’ def add(taskid) a = self.new a.task_id << taskid a.save end class Task include MongoMapper::Document key :name, String many :users end

如何在创建关联记录后正确创建关联记录?

我正在使用Ruby on Rails 4.1,我想知道它是否良好以及在创建关联器记录之后创建关联记录的缺点是什么 。 也就是说,例如,我有Article类和belongs_to article的Comment类,我想在创建文章后创建一个“默认”注释。 可能我可以使用回调方法来实现这一点,但还有其他方法可以实现我想要的吗? 我应该看什么是“平衡的”(例如:类/模块依赖)?

HABTM重复记录

我有2个Game和Theme模型,他们有一个has_and_belongs_to_many关联。 我已经尝试了许多解决方案来防止games_themes表中的重复记录,但没有解决方案可行。 问题是, games_themes是一个表,但它不是一个模型,所以我无法找到一种方法来有效地运行validation。 这是我试过的解决方案 class Theme true end class Game true end