ruby-on-rails-3.2

belongs_to association loaded individually even after eager loading

五迷三道 提交于 2019-12-05 22:12:37
I have the below association class Picture < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :pictures end And in my PicturesController i am eager loading the user class PicturesController < ApplicationController def index @pictures = Picture.includes(:user).all end end In my view, i am displaying each pictures user name -@pictures.each do |picture| %tr %td= picture.name %td= picture.user.name Now the question is, even though i am eager loading the user, i see individual queries fired while displaying the user name in the view. I have around 1000 picture

SQL Like operator in ruby on rails

旧城冷巷雨未停 提交于 2019-12-05 21:24:20
问题 I had got a task to select search the students those name start with param value and city in the selected value.How can i set in ruby on rails? i did like this but this is not working controller def list studentcount=Student.count() puts studentcount @studentname = Student.where("name name1 AND city = :cityId1", {:name1 => params[:name], :cityId1 => params[:cityId]}) puts 'studentname' puts @studentname.inspect @students = Student.limit(params[:jtPageSize]).offset(params[:jtStartIndex]).order

Can the Liquid Ruby template engine deal with Rails forms?

社会主义新天地 提交于 2019-12-05 19:57:29
I've been searching for a template engine to allow users to create lessons and exercises online easily. Seems like Liquid is the most popular for use in Rails. Can Liquid users easily create rails forms? Normally I create forms in ERB with: <%= form_for(@lesson) do |f| %> <% if @lesson.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@lesson.errors.count, "error") %> prohibited this lesson from being saved:</h2> <ul> <% @lesson.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div>lots of fields</div> <% end %> Rails will automatically

Rails 3.2: Replace null values with empty string from json serialization

落花浮王杯 提交于 2019-12-05 18:38:10
I am using Rails 3.2 serialization to convert ruby object to json. For Example, I have serialized ruby object to following json { "relationship":{ "type":"relationship", "id":null, "followed_id": null } } Using following serialize method in my class Relationship < ActiveRecord::Base def as_json(opts = {}) { :type => 'relationship', :id => id, :followed_id => followed_id } end I need to replace null values with empty strings i.e. empty double quotes, in response json. How can I achieve this? Best Regards, zsquare Probably not the best solution, but inspired by this answer def as_json(opts={})

deep nested accepts_nested_attributes_for not rendering attribute data in edit template

爷,独闯天下 提交于 2019-12-05 18:26:05
I'm having a bit of a challenge with understanding accepts_nested_attributes_for. I have a Resident class that is composed of various child classes (address, doctor, guardian, etc). My understanding of the steps necessary to make accepts_nested_attributes_for work is as follows: Create the necessary association(s) Add accepts_nested_attributes_for :resource to the parent class whose form I will use to commit the nested attributes whitelist nested attributes like so. attr_accessible :address_attributes, :doctor_attributes, etc... In the parent controller, build the associated resources. i.e.

Rails 3.2 f.file_field causes routing error

北慕城南 提交于 2019-12-05 18:22:16
Tested on rails 3.2.12 and 3.2.11. In another rails 3.2.11 project I do not have this issue with f.file_field , but in current one I do and can't find a reason for this strange behaviour, so here is my question. I have a weird problem with update action. Here are relevant parts of code: routes: get "signup" => "users#new", :as => "signup" get "profile" => "users#profile", :as => "profile" resources :users do member do get :activate end end controller: def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) redirect_to user_path(@user), :notice => t('users_controller

Creating unique id for <fieldset> when using form_for Rails Nested Model Form

北战南征 提交于 2019-12-05 18:12:28
I have a nested model form in the style of this Railscast http://railscasts.com/episodes/196-nested-model-form-revised?view=asciicast I need to give each tag a unique ID. Currently, each field generated has a unique ID and name given by a helper method that assigns a unique ID to every association record. So that is taken cared of. However, this form has "fieldset" tags which isn't assigned an ID. I need a unique ID for each fieldset for jQuery manipulation purposes. Specifically, how do I do give each fieldset generated for an "Activity" record a unique CSS tag ID? Posted below is how my form

Symbols in query-string for elasticsearch

二次信任 提交于 2019-12-05 17:38:20
问题 I have "documents" (activerecords) with an attribute called deviations. The attribute has values like "Bin X" "Bin $" "Bin q" "Bin %" etc. I am trying to use tire/elasticsearch to search the attribute. I am using the whitespace analyzer to index the deviation attribute. Here is my code for creating the indexes: settings :analysis => { :filter => { :ngram_filter => { :type => "nGram", :min_gram => 2, :max_gram => 255 }, :deviation_filter => { :type => "word_delimiter", :type_table => ['$ =>

Ruby class naming convention with double colon

自作多情 提交于 2019-12-05 16:54:39
I know the :: in Ruby is a scope resolution operator to access methods within modules and classes, but is it proper to name classes using :: ? Example class Foo::Bar::Bee < Foo::Bar::Insect def a_method [...] end end If by “proper” you mean syntactically correct — yes . There's nothing inherently wrong with doing it, and if you're defining a subclass in a separate file (example below) then it's a relatively common practice. # lib/foo.rb module Foo end # lib/foo/bar.rb class Foo::Bar end I would avoid defining classes this way if you cannot be sure that the parent module or class already exists

how to run rake task in background in rails

强颜欢笑 提交于 2019-12-05 16:47:34
This is my command bundle exec rake resque:work QUEUE="*" --trace I want to run this command on my server as a background process. please help me. A method I often use is: nohup bundle exec rake resque:work QUEUE="*" --trace > rake.out 2>&1 & This will keep the task running even if you exit your shell. Then if I want to just observe trace output live, I do: tail -f rake.out And you can examine rake.out at any time. If you need to kill it before completion, you can find it with ps and kill the pid. Just in case somebody finds this 4 years later, bundle has an elegant way of doing this now. For