Tag: 模型

ruby on rails has_many:通过关联,其中有两列具有相同的模型

我有以下型号: class UserShareTag “User” belongs_to :post belongs_to :sharee, :class_name => “User” validates :sharer_id, :presence => true validates :sharee_id, :presence => true validates :post_id, :presence => true end 在Post模型中,我有以下几行: has_many :user_share_tags, :dependent => :destroy has_many :user_sharers, :through => :user_share_tags, :uniq => true, :class_name => “User” has_many :user_sharees, :through => :user_share_tags, :uniq => true, :class_name => “User” […]

嵌套模型错误消息

我正在使用Ruby on Rails 3.0.9,我正在尝试validation嵌套模型。 假设我为“main”模型运行validation并为嵌套模型生成一些错误,我得到以下结果: @user.valid? @user.errors.inspect # => {:”account.firstname”=>[“is too short”, “can not be blank”], :”account.lastname”=>[“is too short”, “can not be blank”], :account=>[“is invalid”]} 如何看待RoR框架会创建一个errors哈希,其中包含以下键: account.firstname , account.lastname , account 。 由于我想通过JavaScript(BTW:我使用jQuery)处理那些涉及CSS属性的错误键值对我在前端内容上显示错误消息,我想“准备”该数据并将这些键更改为account_firstname , account_lastname , account (注意:我用.替换. )。 如何将密钥值从例如account.firstname更改为account_firstname ? 而且, 最重要的是 ,我应该如何处理这种情况? 我正在尝试以“好”方式处理嵌套模型错误? 如果不是,这样做的常见\最佳方法是什么?

在两个Rails项目之间共享模型 – 使用git子模块?

我有一个Rails网站,它被分为两个独立的项目 – 公共站点和管理站点。 由于两个站点都使用相同的数据库,因此应用程序之间共享模型(实际上现在它们是重复的)。 我在这里遇到的问题是,当公共项目中发生模型更新时,我需要将更改复制到管理项目中。 我已经看了一下SO,并注意到有一个问题有答案暗示使用svn:external或git子模块,但我不完全确定如何做到这一点。 基本上我的目标是只能在一个地方进行更改,将这些更改提交给git ,然后当我需要更新时,能够在其他项目中提取更改。

Rails 3,什么是最好的树行为?

所以我正在寻找一个可以为页面模型带来一些树行为的gem。 从我所看到的有很多,我想知道其他人的想法。 如果有任何伟大的或狡猾的避免。 干杯。

通过多个级别的Rails关联

我对Rails相对较新,正在开发一个涉及多个“嵌套”数据级别的项目。 我在制定正确的关联方面存在问题,因此我可以将模型的所有子元素提高3级。 以下是模型的示例: class Country < ActiveRecord::Base has_many :states end class State < ActiveRecord::Base belongs_to :country has_many :cities end class City < ActiveRecord::Base belongs_to :state has_many :people end class Person < ActiveRecord::Base belongs_to :city end 我已经在Country模型中实现了一个关系, has_many :cities, :through => :states并且尝试调用Country.first.cities.all ,这有效。 但是,当我在People控制器中尝试Country.first.cities.all.people.all时,我在访问给定国家/地区的所有人时遇到问题。 处理这种关联情况的最佳方法是什么? 我应该为每个子表添加一个外键,例如country_id ,以便我可以获取一个Country所有People ? 任何建议,将不胜感激。

rails在模型中validation值是否在数组内

我有一个表单,我传递一个名为 :type的字段 ,我想检查它的值是否在允许类型数组中,以便不允许 任何人发布不允许的类型 。 数组看起来像 @allowed_types = [ ‘type1’, ‘type2’, ‘type3’, ‘type4’, ‘type5’, ‘type6’, ‘type7’, etc… ] 我尝试过使用 validates_exclusion_of或validates_inclusion_of但它似乎不起作用

Rails:对于每个模型,始终包含created_at的毫秒数

如何修改我的Rails应用程序以始终包含我的模型的created_at字段的毫秒信息? 这个问题有关于如何为单个模型做到这一点的答案,但我想在全球范围内进行。 例如,当我检索所有的Item模型时(通过使用GET命中/items ),我得到以下JSON: [{“created_at”:”2011-08-07T23:42:15Z”,”updated_at”:”2011-08-07T23:42:15Z”,”id”:180,”user_id”:6,”content”:”test”}] 但请注意, created_at字段没有关于它创建的毫秒数的任何信息。 如何为我的所有模型添加该function?

rubyMine’无法将测试报告者附加到测试框架’

运行所有模型测试时,检测到rubyMine’MiniTest框架’错误。 我可以在常规命令行运行所有模型测试。 例如rake spec:models 当我使用rubyMine时: 我可以运行一个模型测试。 但是,当我尝试在模型中运行所有测试时,我得到了 MiniTest framework was detected. It is a limited version of original Test::Unit framework. RubyMine/IDEA Ruby plugin test runner requires full-featured version of the framework, otherwise default console tests reporter will be used instead. Please install ‘test-unit’ gem and activate it on runtime. 我尝试将’test-unit’添加到我的Gemfile并重新绑定,但现在得到: `/home/durrantm/.rvm/rubies/ruby-1.9.2-p180/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/durrantm/Downloads/RubyMine-3.2.3/rb/testing/runner/tunit_in_folder_runner.rb Testing […]

Rails:如果满足某些条件,我如何运行before_save?

我有一个我调用的before_save方法重命名上传的图像。 before_save :randomize_file_name def randomize_file_name extension = File.extname(screen_file_name).downcase key = ActiveSupport::SecureRandom.hex(8) self.screen.instance_write(:file_name, “#{key}#{extension}”) end 该方法是我的Item模型的一部分。 当我创建一个新项目或需要更新与项目相关联的图像时,这很有用……但问题是,如果我需要更新项目而不是图像,则randomize_file_name方法仍会运行并重命名文件数据库(虽然不是文件本身,显然)。 所以,我想我需要想办法只运行randomize_file_name如果一个文件包含在表单提交中…但我不知道如何解决这个问题。

如何确保不通过activerecords将重复的行添加到我的数据库表中?

所以我有一张叫做投票的桌子。 有三列。 VoterID,VoteforID和投票。 每个单独列的唯一性并不重要,但是同一行不能重复两次 例如。 这是对的 Voter_id | Votefor_id | 投票 1 | 2 | 1 1 | 3 | 1 1 | 4 | 1 2 | 1 | 2 这是错的。 Voter_id | Votefor_id | 投票 1 | 2 | 1 1 | 2 | 1 1 | 4 | 1 2 | 1 […]