ruby-on-rails-3

get model attribute dynamically in rails 3

允我心安 提交于 2019-12-23 07:21:56
问题 How can I get the attribute from the model object dynamically? I have the attribute of the User object as string: u = User.find(1) Can I do something like u.get("user_id") 回答1: You could try using the ActiveRecord model instance like a hash. u = User.find(1) name = u[:name] field = "first_name" first_name = u[field] 回答2: Yet another approach: attr = :first_name @user.read_attribute attr 回答3: Try this user = User.find(1) then something like this should do what you require user.send('field_name

Rails 3: What is the default order of “MyModel.all”?

会有一股神秘感。 提交于 2019-12-23 07:16:16
问题 I know that Job.all returns an array of all jobs. But, what would be the order ? Are they ordered by ascending id ? What Job.first returns ? The documentation says: "Returns the first resource found." But, what is the looking order ? 回答1: Pretty sure default order is however the DB decided to return them. See here for more info. ActiveRecord Find All not sorting by ID? If you want them in a specific order, you should do Model.order() 回答2: There is no order. You should watch your logs while

rails g scaffold for existing model and DB table

不羁的心 提交于 2019-12-23 07:16:16
问题 I would like to create a structure with rails g scaffold Article , but I have already created the table Articles and model Articles . Is there any way to do that? 回答1: rails generate scaffold_controller Article 回答2: Use rails generate scaffold_controller Article with parameters, example: rails g scaffold_controller Article title:string body:text Create views with attributes in tables 来源: https://stackoverflow.com/questions/9808668/rails-g-scaffold-for-existing-model-and-db-table

Rails 3: What is the default order of “MyModel.all”?

陌路散爱 提交于 2019-12-23 07:16:11
问题 I know that Job.all returns an array of all jobs. But, what would be the order ? Are they ordered by ascending id ? What Job.first returns ? The documentation says: "Returns the first resource found." But, what is the looking order ? 回答1: Pretty sure default order is however the DB decided to return them. See here for more info. ActiveRecord Find All not sorting by ID? If you want them in a specific order, you should do Model.order() 回答2: There is no order. You should watch your logs while

How to read content of remote file in Ruby on Rails?

断了今生、忘了曾经 提交于 2019-12-23 07:09:26
问题 here my file: http://example.com/test.txt i have to read content of http://example.com/test.txt (a JSON string) and parse it in Ruby 回答1: I would suggest using open-uri: require 'json' require 'open-uri' result = JSON.parse open('http://example.com/data.json').read 回答2: require 'net/http' uri = URI('http://my.json.emitter/some/action') json = Net::HTTP.get(uri) json will contain the JSON string you fetched from uri . Then read this StackOverflow post. 来源: https://stackoverflow.com/questions

Using number_with_precision on controller in rails3

旧巷老猫 提交于 2019-12-23 07:03:17
问题 I want to use this helper method on controller. Any way to achieve this? 回答1: Probably not a great idea, but if you must, include the helper like so: class WhateverController include ActionView::Helpers::NumberHelper def show render :text => number_with_precision(2342.234, :precision => 2) end end 回答2: A much better way to control number precision is rails controllers is to use sprintf "%.2f" % 2342.234 => "2342.23" "%.6f" % 2342.234 => "2342.234000" 来源: https://stackoverflow.com/questions

Active Admin: How to set page title?

无人久伴 提交于 2019-12-23 06:51:07
问题 This seems like it should be relatively simple, but I've had some trouble tracking down the answer: How do you set the page title in ActiveAdmin? 回答1: Consolidating answers and adding a little: Most of this is on this page on the wiki (or I'll put it there soon). Within the file that registers your model for activeadmin (e.g. app/admin/user.rb), you can have ActiveAdmin.register User do # a simple string index :title => "Here's a list of users" do ... end # using a method called on the

ERROR Errno::ECONNRESET: Connection reset by peer

走远了吗. 提交于 2019-12-23 06:50:14
问题 gem 'rails', '3.0.0' gem 'devise' gem 'bson_ext', '>= 1.0.7' gem 'bson', '>= 1.0.7' gem 'mongo_mapper', :branch => 'rails3', :git => 'http://github.com/jnunemaker/mongomapper.git' gem 'devise-mongo_mapper', :git => 'git://github.com/collectiveidea/devise-mongo_mapper' With the above setup I get the following errors on requests: Started GET "/users/sign_out" for 127.0.0.1 at 2010-09-27 13:16:30 +0300 Processing by Devise::SessionsController#destroy as HTML Redirected to http://localhost:3000/

Rails - Given @Comments - how to obtain the 2nd to last comment w/o rehitting the db

南楼画角 提交于 2019-12-23 06:48:27
问题 given @comments which can contain 1 or more records. How do I obtain the 2nd to last comment? Thanks 回答1: I suppose that would also be called the penultimate record: @comments[-2] Here are the docs 1 to Ruby's interesting index operator. 1. Rubyists, note how I linked this. If you take the version out of your ruby-doc links the reference will be durable and recent. 回答2: With any Ruby array, you can specify any range or specific index, for example if you wanted every comment from the second

1) PasswordResets emails user when requesting password reset

南笙酒味 提交于 2019-12-23 06:35:06
问题 I've been trying to add a password reset for users that forget their password. The users click on forgot password? on the sign up page. Then the user types their email and clicks reset password, which creates a token and sends an email with a link to reset their password. For the most part it works ONLY when the email box is blank or has exactly 6 random letter/numbers, but it doesn't work when a user puts in his email and clicks password reset, it brings up the error message: **Validation