Tag: ruby on rails 3

CSV读取v / s从数据库读取的临时表,循环优化和活动记录使用情况。 ruby

文件的CSV解析非常慢,所以我试图直接将文件加载到数据库中的某个临时表中,然后执行如下计算: 之前就是这样,花了13分钟使用以下方法添加条目: CSV.foreach(fileName) do |line| completePath = line[0] num_of_bps = line[1] completePath = cluster_path+ ‘/’ + completePath inode = FileOrFolder.find_by_fullpath(completePath, :select=>”id”) metric_instance = MetricInstance.find(:first, :conditions=>[“file_or_folder_id = ? AND dataset_id = ?”, inode.id, dataset_id]) add_entry(metric_instance.id, num_of_bps, num_of_bp_tests) end def self.add_entry(metaid, num_of_bps, num_of_bp_tests) entry = Bp.new entry.metric_instance_id = metaid entry.num_of_bps = num_of_bps entry.num_of_bp_tests = num_of_bp_tests entry.save return […]

根据用户历史自定义交易应用程序的主页和电子邮件:如何在Rails / postgreSQL上以正确的方式执行此操作?

我想定制: 1.主页上的交易顺序 2.电子邮件取决于用户看到的交易。 感谢SO上的人 ,似乎最好的是拥有3个模型和表“standard_user”,“deals”和“deals_participation”,以便拥有应用所需的多对多关系 ,链接表与如下: class DealParticipation < ActiveRecord:Base #This means the deal_participations table has a standard_user_id key belongs_to :standard_user #This means the deal_participations table has a deal_id key belongs_to :deal #… more logic goes here … end class StandardUser :deal_participations # … more logic goes here … end class Deal :deal_participations belongs_to :admin_user […]

如何在Rails中获取页面的数据/视图统计信息?

我想知道,如何在Rails中获得这些统计数据? / page / 1,/ page / 2这样的页面有多少独特的视图 他们来自哪个国家? 因为我想在这些页面下显示这些信息,我不想使用任何外部库,我希望我们可以有一些ruby gem可以做到这一点 谢谢 编辑1 – 我可以看到1-2个gem,如https://github.com/jkrall/analytical但仍然想要问SO社区 编辑2 – 我不认为上面的gem是我正在寻找它看起来它只是将第三方分析插入现有的应用程序

未定义的方法`end_with?’ for“rake aborted!”:String(NoMethodError)

每当我键入rake db:migrate或rake -T时,我一直面临运行rake任务/命令的问题,它显示错误说未定义的方法`end_with?’ for“rake aborted!”:String(NoMethodError)。使用ruby186,rails238和gem version 1.3.3 rake db:migrate /home/projects/.rvm/gems/ruby-1.8.6-p420@global/gems/rubygems-bundler-1.1.0/lib/rubygems-bundler/regenerate_binstubs_command.rb:48: warning: parenthesize argument(s) for future version /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/lib/rake/trace_output.rb:14:in `trace_on’: undefined method `end_with?’ for “rake aborted!”:String (NoMethodError) from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/lib/rake/trace_output.rb:14:in `map’ from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/lib/rake/trace_output.rb:14:in `trace_on’ from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/lib/rake/application.rb:322:in `trace’ from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/lib/rake/application.rb:176:in `display_error_message’ from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/lib/rake/application.rb:169:in `standard_exception_handling’ from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/lib/rake/application.rb:70:in `run’ from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/gems/rake-10.0.3/bin/rake:33 from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/bin/rake:19:in `load’ from /home/a3logics/.rvm/gems/ruby-1.8.6-p420@global/bin/rake:19 from /home/a3logics/.rvm/gems/ruby-1.8.6-p420/bin/ruby_noexec_wrapper:14 我被困在这里的任何帮助。

使用select,group和Rails查询将无法正常工作

@violators = User.includes(:violation).select(‘users.id, violations.id’) .group(‘users.id, violations.id’).having(“COUNT(violations.user_id) >= ?”, 5) .order(“COUNT(violations.user_id) desc”) 每个用户都有很多违规行为。 通过上面的查询,我可以订购违规次数最多的用户,但我只想查看有5次或更多次违规的用户。 当我插入5时,尽管存在超过5次违规的用户,但没有显示任何内容。 当我在那里输入1时,所有有违规行为的用户都会出现! 更新: 我无法按照我之前的建议订购违规次数最多的用户。 也许实际的违规计数存在问题。 它不知何故没有抓住它们。 虽然当我检查控制台并尝试运行 – user.violation.count时,它会给我准确的计数。

如何从Rails模型中获取属性的数据类型?

我在Rails项目中使用Postgres,我发现我需要将所有varchar数据类型更改为citext 。 我想要创建一个循环遍历所有模型及其属性的迁移,并根据需要转换它们,而不是手动执行此操作。 大多数这些模型都是空的,因此不是实例化它们的问题。 我需要找出ActiveRecord是否“知道”其相应数据库列的数据类型是什么。

在生成mass_assignment错误之前,使用Hash#exclude排除控制器操作中的嵌套表单字段?

我目前在我的销售控制器中有一个创建操作,如下所示: def create @sale = Sale.new(params[:sale].except(:vehicles_attributes)) if @sale.save redirect_to @sale, :notice => “Successfully created sale.” else render :action => ‘new’ end end 目的是排除一些仅用于填充链接选择的属性,不应提交(没有列的列)。 使用上面的控制器代码,我发现参数仍包含”sale”=>{“vehicles_attributes”=>{“0″=>{“make”=>””,”model”=>””}}}所以我似乎错过了控制器代码中的一些东西。 编辑:经过一些更深入的挖掘,我发现mass_assignmentexception在我的除了代码有机会删除不应该由表单发送的参数之前触发,所以我回到原点。 在获取mass_assignment错误之前,如何确保删除表单不应发送的字段?

在Ruby on Rails中放置预定义类(例如String,Symbol)扩展的正确位置在哪里?

我想将my_method添加到Symbol类,并且能够从app/helpers/application_helper.rb调用my_method : module ApplicationHelper def my_helper my_symbol.my_method end end 最适合放置的地方在哪里: class Symbol def my_method end end ?

如何实现具有3个固定分类的jobpostfunction

我想要做的是创建放置模型,用于发布3个固定类别的工作,即实习,公司和自由职业。将有一个安置菜单,将有3个menues作为实习,公司和自由职业者,并根据用户选择,对于这3个类别,将显示相同的职位发布表格以创建职位。 在显示工作岗位时,将根据类别显示不同的视图。 我没有得到我应该如何实现这一点。我是否应该创建位置和类别作为一个不同的模型,并给予关联作为多个类别的位置和属于类别的位置,但如果我这样做,在类别模型我有3固定的选择,我不会接受用户的选择,那么我如何在模型中添加这3个选项将被修复并按类别添加它们的工作岗位? 我该如何实现这个展示位置?

当我在新项目上尝试命令rails server时,我收到此错误

我正在运行Debian 7.我正在使用RVM。 我也在运行mySQL。 我是ruby的新手以及rails。 如果我看起来很天真,请原谅我。 我设置了一个项目: rails new projectname -d mysql 然后我进入该目录并运行: rails server 然后我得到这个错误: /home/user/.rvm/gems/ruby-2.0.0-p247/gems/execjs-2.0.0/lib/execjs/runtimes.rb:51:in `autodetect’: Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/execjs-2.0.0/lib/execjs.rb:5:in `’ from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/execjs-2.0.0/lib/execjs.rb:4:in `’ from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require’ from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `’ from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require’ from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `’ from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require’ from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in […]