Tag: 的Ruby on 轨道

Ruby on Rails应用程序上的AssociationTypeMismatch错误

我在rails应用程序上的ruby有问题。 我有两个模型 – “患者”和“地址”,患者有一个地址,地址属于患者。 Patient.rb class Patient < ActiveRecord::Base has_many :charge_slips has_one :address validates_presence_of :last_name validates_presence_of :first_name validates_presence_of :middle_name end Address.rb class Address < ActiveRecord::Base belongs_to :patient validates_associated :patient end 患者controller.rb class PatientController ‘index’ else redirect_to :action => ‘new’ end redirect_to :action => ‘index’ else redirect_to :action => ‘new’ end end end new.html.rb { :action […]

有什么好的Ruby on Rails博客?

我6个月前开始学习Rails,到目前为止,我喜欢它背后的社区。 有大量的论坛和文档资源可供使用,但我无法找到一个教学,一贯维护的Rails博客。 我喜欢Ryan Bates的Railscasts,但我很想找到另一个与他的一致性或质量相同的博客。 有什么建议? 谢谢。

Ruby on Rails – 有很多,通过:找到多个条件

我意识到很难用单词来解释我的问题,所以我将用一个例子来描述我想要做的事情。 例如: #model Book has_many: book_genres has_many: genres, through: :book_genres #model Genre has_many: book_genres has_many: books, through: :book_genres 因此,只查找属于一种类型的书籍会相对简单,例如: #method in books model def self.find_books(genre) @g = Genre.where(‘name LIKE ?’ , “#{genre}”).take @b = @g.books #get all the books that are of that genre end 所以在rails控制台中我可以做Book.find_books(“Fiction”) ,然后我会得到所有fiction类型的书。 但是我怎样才能找到所有既是“年轻人”又是“小说”的书呢? 或者,如果我想查询有3种类型的书籍,例如“青少年”,“小说”和“浪漫”,该怎么办? 我可以做g = Genre.where(name: [“Young Adult”, “Fiction”, […]

如何在RoR中初始化Model对象?

我正在创建一个网站,我希望每个用户都可以从属性的某些值开始。 这是class级: class User < ActiveRecord::Base attr_accessible :name, :email, :goal, :measurement, :bmr_formula, :fat_factor, :protien_factor end 在rails console –sandbox中,我可以更改值。 但我想用某些值启动对象。 例如,我希望测量以“US”开头,bmr_formula以“Katch”开头……等等而不是nil。 现在,一切都从零开始。 我将继续展示我尝试过每次尝试得到的结果。 这是有效的: after_initialize do self[:measurement] = “US” self[:bmr_formula] = “katch” self[:fat_factor] = 0.655 self[:protein_factor] = 1.25 puts “User has been initialized!” end 1.9.3p125 :001 > user = User.new User has been initialized! => # […]

如何在Ruby on Rails 3中搜索Active Record的文本?

如何在Ruby on Rails中搜索字符串? 例如,包含“text”的列的所有记录。 Active Record是否有方法或者我必须使用SQL“LIKE”吗?

如何列出包括关注点在内的所有模型

有没有一种简单的方法来列出包含特定关注点的所有模型的类名? 就像是: ActiveRecord.models.select{ |m| m.included_modules.include? MyConcernModule }

在rails中一次更新多条记录

在我正在构建的rails 2应用程序中,我需要更新具有特定属性的记录集合。 我有一个命名范围来查找集合,但我必须迭代每个记录以更新属性。 我不得不进行一次查询来更新数千条记录,而是要进行数千次查询。 到目前为止我发现的是Model.find_by_sql(“UPDATE products …) 这感觉真的很年轻,但我用Google搜索并环顾四周,并没有找到我的答案。 为清楚起见,我所拥有的是: ps = Product.last_day_of_freshness ps.each { |p| p.update_attributes(:stale => true) } 我想要的是: Product.last_day_of_freshness.update_attributes(:stale => true)

Rails 3:延迟加载与急切加载

在Rails 3中,它们是相同还是不同? 他们有什么不同? o = Appointment.find(297) o.service o = Appointment.includes(:service).find(297) o.service

Active Record运行所有查询两次

不知道为什么在生产中发生这种情况: 更新这是来自rails控制台: User.all User Load (0.5ms) SELECT “users”.* FROM “users” User Load (0.5ms) SELECT “users”.* FROM “users” 2.0.0-p451 :005 > User.first.destroy User Load (0.6ms) SELECT “users”.* FROM “users” ORDER BY “users”.”id” ASC LIMIT 1 User Load (0.6ms) SELECT “users”.* FROM “users” ORDER BY “users”.”id” ASC LIMIT 1 (0.2ms) BEGIN (0.2ms) BEGIN Discussion Load (60.4ms) SELECT […]

你应该要求’minitest / autorun’代替。 在铁轨上的ruby

我正在rails上创建ruby上的应用程序: 创建用户后,为了validation用户,我在命令下运行: $ bundle exec rake db:migrate $ bundle exec rake test:prepare 这两个命令都在rails命令行上正常运行,但是当我运行以下命令时: $ bundle exec rspec spec/models/user_spec.rb 我收到以下错误: Warning: you should require ‘minitest/autorun’ instead. Warning: or add ‘gem “minitest”‘ before ‘require “minitest/autorun”‘ 我正在使用4.1.1版本的rails 。 我不明白为什么会这样。 请建议我,等待你的回复。 谢谢。