我正在开发一个使用Datamapper作为其ORM的Rails 3.2应用程序。 我正在寻找一种通过关联模型的属性对结果集进行排序的方法。 具体来说我有以下型号: class Vehicle include DataMapper::Resource belongs_to :user end class User include DataMapper::Resource has n, :vehicles end 现在我希望能够查询车辆并按驱动程序的名称对它们进行排序。 我尝试了以下但似乎没有使用Datamapper: > Vehicle.all( :order => ‘users.name’ ) ArgumentError: +options[:order]+ entry “users.name” does not map to a property in Vehicle > Vehicle.all( :order => { :users => ‘name’ } ) ArgumentError: +options[:order]+ entry [:users, “name”] of […]
在我的Rails应用程序中,我只要求用户在注册时输入电子邮件和姓名,然后为他们提供选项,以便为他们的个人资料提供更全面的联系详细信息。 因此,我有一个User.rb模型,它与Contact.rb有关联,即 User.rb has_one :contact Contact.rb belongs_to :user Contact.rb具有您可能期望的可预测字段,例如地址,邮政编码等,但它也存储了省份与省份.rb模型的关系,所以 Contact.rb attr_accessible :address, :city, :mobile, :postalcode, :province_id, :user_id belongs_to :user belongs_to :province Province.rb has_many :contacts 我这样做(而不是将省名称作为“字符串”存储在contact.rb上),这样我就可以更轻松地(所以我认为)按省份对用户进行分类。 在其中一个artists_controller的show动作中,我执行以下操作来检查用户是否尝试按省排序,然后调用一个执行搜索的artists_by_province方法 if params[:province_id] province = params[:province_id] province = province.to_i #convert string to integer @artistsbyprovince = User.artists_by_province(province) else @artists = User.where(:sculptor => true) end 这是User.rb模型上的方法,如果传入省id,它将调用该方法 scope :artists_by_province, lambda {|province| joins(:contact). where( […]
我在设置我的网络应用程序以使用Windows Live Hosted电子邮件而不是普通的Google Apps电子邮件时遇到问题。 这是因为谷歌正在收取此类服务的费用。 我输入了正确的config.action_mailer.smtp_settings,但由于某种原因,我无法收到正确发送的电子邮件通知。 我的配置如下,如果我将配置与另一个Google Apps配置设置电子邮件交换,它就可以正常运行。 我错过了什么吗? config.action_mailer.smtp_settings = { :enable_starttls_auto => true, :address => “smtp.live.com”, :port => “587”, :domain => “mail.live.com”, :user_name => “###########.net”, :password => “###########”, :authentication => :plain } 这是我收到的错误。 getaddrinfo:提供nodename或servname,或者不知道
我知道在osx上使用postgres有点困难,因为它有自己的版本运行,所以为了psql到它你需要提供像这样的主机名 psql -h localhost mydatabasename -U me 但是,如何为您的database.yml文件执行此操作以获取所有轨道可爱性。 如果您尝试连接 development: adapter: postgresql username: me database: mydatabasename 你收到了经典 Couldn’t create database for {“adapter”=>”postgresql”, “username”=>”me”, “database”=>”mydatabasename”} could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/tmp/.s.PGSQL.5432”? 关于如何提供信息或配置osx的任何想法都不要吮吸这么多?!
我正在尝试利用ice_cube和recurring_selectgem的[超棒]function来处理重复发生的事件。 我的数据库中有一个schedule (text)列,事件模型中有以下内容: def schedule=(new_schedule) write_attribute(:schedule, RecurringSelect.dirty_hash_to_rule(new_schedule).to_yaml) end def converted_schedule Schedule.from_yaml(self.schedule, :start_date_override => self.start_date) end 查看psql中的schedule列,它似乎正确地存储了日程表。 这是我的表格: .control-group = f.label ‘Date’, :class => ‘control-label’ .controls = f.text_field :start_date, :class => ‘datepicker’ .control-group = f.label ‘Recurring?’, :class => ‘control-label’ .controls = f.select_recurring :schedule, :allow_blank => true 但是,当我尝试输出converted_schedule ,它只显示开始日期,不会显示任何其他日期。 我有一些怀疑,我没有成功修改…也许YAML没有正确转换为converted_schedule方法? 也许我需要一个结束日期(我不知道recurring_select上有哪些function可用)?
我使用这个脚本在Rails 3.2.8应用程序中使用HTML5 FormData上传文件(逐个)。 http://jsfiddle.net/RamPr/ $(‘.uploader input:file’).on(‘change’, function() { $this = $(this); $(‘.alert’).remove(); $.each($this[0].files, function(key, file) { $(‘.files’).append(” + file.name + ”); data = new FormData(); data.append(file.name, file); $.ajax({ url: $(‘.uploader’).attr(‘action’), contentType: ‘multipart/form-data’, type: ‘POST’, dataType: ‘json’, data: data, processData: false }); }); }); 但是当我上传文件时,我在控制台中收到此错误: webrick/server.rb:191:in `block in start_thread’ ERROR ArgumentError: invalid %-encoding (“filename.jpeg” Content-Type: image/jpeg […]
我正在使用prawn gem生成PDF报告, @user.description returns as string “sample text &nspb; sample text” 同时将值附加到pdf表 pdftable = Prawn::Document.new pdftable.table([[“#{@user.description}”]], :column_widths => {0 => 50, 1 => 60, 2 => 280, }, :row_colors => [“ffffff”]) 在这种情况下生成的pdf具有带有html标签的内容,即使我尝试应用html_safe但它不是转义标签。 是否有可能在prawn pdftable中使用/ apply html_safe,以逃避html标签?
看起来Mongoid现在是基于性能和开发活动的Mongo的优秀ORM。 不幸的是,我们使用MongoMapper并需要迁移。 我们应该注意哪些问题或绊脚石? 我们在Google上发现了一些过时的文章,并尝试在Mongoid Google网上论坛上发帖(虽然我们被禁止),但是会喜欢过去做过这些的SO成员的想法。 我们在Rails 3.2.12上。 谢谢!
我对Rails相对较新,有点惊讶这不是一个可配置的行为……至少没有一个我能找到的?!? 我原以为99%的表单会受益于从所有string和text字段中修剪的空白?!? 猜猜我错了…… 无论如何,我正在寻找一种干燥的方法来从Rails 3应用程序中的表单字段(类型:string&:text)中删除所有空格。 视图有自动引用(包含?)并可用于每个视图的助手……但模型似乎没有这样的东西?!? 或者他们呢? 所以目前我做的是首先 要求的 , 然后 包括 whitespace_helper(又名WhitespaceHelper)。 但这对我来说似乎仍然不是很干,但它有效…… ClassName.rb: require ‘whitespace_helper’ class ClassName < ActiveRecord::Base include WhitespaceHelper before_validation :strip_blanks … protected def strip_blanks self.attributeA.strip! self.attributeB.strip! … end LIB / whitespace_helper.rb: module WhitespaceHelper def strip_whitespace self.attributes.each_pair do |key, value| self[key] = value.strip if value.respond_to?(‘strip’) end end 我想我正在寻找一个单一的(DRY)方法(类?)来放置一个( lib/ ?),它将获取一个params(或属性)列表并从每个属性w中删除空格( .strip! […]
假设您构建了一个涉及多个方法链接的查询,例如 Post.where(‘id > 10’).limit(20).order(‘id asc’).except(:order) 我想知道幕后发生了什么? 据推测,链的每个部分都有助于构建一个SQL SELECT,一旦链完成,就会执行语句,创建模型等。它如何“知道”链的末端在哪里? 每个方法都返回一个创建SQL片段的ActiveRecord :: Relation吗?