ruby-on-rails-3.2

first_or_create by email and then save the nested model

扶醉桌前 提交于 2019-12-12 07:56:21
问题 I two models User and Submission as follows: class User < ActiveRecord::Base # Associations has_many :submissions accepts_nested_attributes_for :submissions # Setup accessible (or protected) attributes for your model attr_accessible :email, :name, :role, :submission_ids, :quotation_ids, :submissions_attributes validates :email, :presence => {:message => "Please enter a valid email address" } validates :email, :uniqueness => { :case_sensitive => false } end class Submission < ActiveRecord:

Using update_columns in Rails 3?

和自甴很熟 提交于 2019-12-12 07:44:43
问题 Is there a shorter way for this in Rails 3? user.update_column(:attribute1, value1) user.update_column(:attribute2, value2) user.update_column(:attribute3, value3) user.update_column(:attribute4, value4) I tried update_columns but it's only available in Rails 4. Thanks for any help. 回答1: Here's a workaround for Rails 3.x: User.where(id: user.id).update_all(attribute1: value1, attribute2: value2, ...) 回答2: If you need speed you can also call execute directly on AR connection. I used something

Adding parameter to a scope

有些话、适合烂在心里 提交于 2019-12-12 07:29:50
问题 I have a ActiveRecord query for example like this: @result = stuff.limit(10) where stuff is a active record query with where clauses, order by, etc... Now I thought why to pass magic numbers like that to the controller? So do you think is it a good practice to define a scope for "limit(10)" and use that instead? and how would the syntax look like? 回答1: The scope would look like any other (although you may prefer a class method), e.g., class Stuff < ActiveRecord::Base def self.lim limit(3) end

ruby on rails how to deal with NaN

北慕城南 提交于 2019-12-12 07:24:34
问题 I have read few posts regarding NaN but did not figure out how to deal with it in Ruby on Rails. I want to check a value if it is a NaN I want to replace it with Zero(0). I tried the following logger.info(".is_a? Fixnum #{percent.is_a? Fixnum}") when percent has NaN it returns me false. I have made few changes in the logger logger.info("Fixnum #{percent.is_a? Fixnum} percent #{percent}") Output Fixnum false percent 94.44444444444444 Fixnum false percent NaN Fixnum false percent 87.0 回答1: NaN

Rails complex has_many and has_many through

拟墨画扇 提交于 2019-12-12 06:12:00
问题 I have 4 models: Drawer, Folder, FolderDocument and Document as follows: class Drawer < ActiveRecord::Base has_many :folders #Drawer has many folders end class Folder < ActiveRecord::Base belongs_to :drawer has_many :folder_documents # Folder has a "version" attribute which reflects the latest version # Use proc to give back latest version by default e.g. folder.documents or folder.documents(5) will give back a specific version. has_many :documents, :through => :folder_documents, :conditions

Rails Dragonfly find files in use

别来无恙 提交于 2019-12-12 06:04:42
问题 We are using Dragonfly for file and image upload in the app and in the Rails Admin. Dragonfly in app part Users can apply for jobs and add their resume as attachment. When user uploads attachments and sucesfully apply for a job the files get deleted. Dragonfly in Rails Admin part Admins can create pages with attachments and images in the Rails Admin part, these attachments are linked via the specific tables. Problem When a user is applying for a job but doesn't finish the apply the files

CSV read v/s Temp table read from database, optimization of the loop and active record usage . Ruby

倖福魔咒の 提交于 2019-12-12 05:36:07
问题 CSV parsing of the file was very slow so I was trying to load the file directly in to some temp table in database directly and then doing the computation as below : Earlier it was like this, took 13 mins to add the entries using below method : 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=>[

Show Last modified tables/records Rails 3.2

心不动则不痛 提交于 2019-12-12 05:25:09
问题 I still can't figure out how to implement this as i am a newbie with this. some people helpt me and said i had to use audited, so it did. this is my controller: def show add_breadcrumb 'Contract Bekijken', :contracten_path @contracten = Contracten.find(params[:id]) @audits = @contracten.audits.collect { |a| a.created_at } respond_to do |format| format.html # show.html.erb format.json { render json: @contracten } end end Here's a pastie of my whole controller. http://pastie.org/4270702 But i

Path method displayed with rake routes is still undefined

有些话、适合烂在心里 提交于 2019-12-12 05:15:08
问题 After getting rid of this question, I get one another. I have a path method specified in routes resources :tree_nurseries do ... . And shows correctly when I do rake routes which gives new_tree_nursery then normally new_tree_nursery_path will be available. But, when I call it with a link_to method, it remains undefiend.... ( new_tree_nursery_path ) What did I missed again? this is the code line : .actions = link_to t('.new', :default => t("helpers.links.new")), new_tree_nursery_path, :class =

Add custom user roles spree 1.3.1

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 05:07:42
问题 I am using spree 1.3.1 and Devise gem for authentication and i need to add a user_role called as " partner " who can see orders in admin area but can't create/edit/update/delete any of the orders. Thanks in advance 回答1: Having app/models/partner_ability.rb file. Then use the following role based read permissions for role partner - class PartnerAbility include CanCan::Ability def initialize(user) user ||= User.new if user.has_role? "partner" can :read, Product end end end Also add the