rails-activerecord

Rails 3: Best practices for saving models

拟墨画扇 提交于 2020-01-04 04:43:47
问题 When should I save my models in rails? and who should be responsible for calling save, the model itself, or the caller? Lets say I have (public)methods like udpate_points , update_level , etc. in my user model. There are 2 options: The model/method is responsible for calling save . So each method will just call self.save . The caller is responsible for calling save. So each method only updates the attributes but the caller calls user.save when it's done with the user. The tradeoffs are fairly

running sanitize_sql_array outside of a model

扶醉桌前 提交于 2020-01-03 21:25:10
问题 I'm writing a script to build some sql queries and I want to sanitize the query before I execute it. When i call sanitize_sql_array on the array I hit an exception, 'undefined method 'sanitize_sql_array''. How do utilize that active record function without calling from within a model? 来源: https://stackoverflow.com/questions/30808148/running-sanitize-sql-array-outside-of-a-model

Rails 4 Postgresql array data-type: updating values

不想你离开。 提交于 2020-01-03 16:54:58
问题 I am just starting to use the array data-type in Postgres with Rails 4 but I am having trouble getting values on an existing array to update. I have a column called 'quantity' that is an array of integers( [0,0] ). I want to update the value at 'quantity[0]' and I have tried the following in the console: a = Asset.find(2) => #<Asset id: 2, quantity: [0,0]> a.quantity[0] = 5 => 5 a.quantity_will_change! => [5, 0] a.save => true a.reload => #<Asset id: 2, quantity: [0,0]> As you can see, the

How can you discover accessible attribute names from a model instance?

妖精的绣舞 提交于 2020-01-03 03:42:10
问题 This answer says you can do object.attribute_names to get a list of attribute names for a model instance. But is there any way to get a list of all its accessible attribute names? 回答1: You can use accessible_attributes . You have to provide a role, because different roles can have different accessible attributes. If you want to have the attributes from a model instance you can use this code: @my_model.class.accessible_attributes(:admin) # Returns array of symbols 来源: https://stackoverflow.com

Render failing to render correct template in rescue_from ActiveRecord::Rollback method

瘦欲@ 提交于 2020-01-02 23:59:24
问题 I'm building the checkout page for an e-commerce site, and I have a fairly long transaction that creates a new User model and a new Order model. I wrapped the creation of these models in a transaction so that if validation for one fails, the other isn't hanging around in the database. Here's the trimmed-down code in my OrdersController: rescue_from ActiveRecord::Rollback, with: :render_new def render_new render action: 'new' end ActiveRecord::Base.transaction do @user = User.new params[:user]

Retrieving a single record from an ActiveRecord/Rails query

不问归期 提交于 2020-01-02 07:48:08
问题 I issue queries like the following that retrieve exactly 0 or 1 records: car = Car.where(vin: '1234567890abcdefg') What's returned is of course a list of cars of length 1. So I end up adding .first at the end of the query: car = Car.where(vin: '1234567890abcdefg').first Seems like there should be a better way in Rails. Does something like .onlyOne exist which would return just a single Car and throw an exception if there was more than 1? car = Car.where(vin: '1234567890abcdefg').onlyOne or

How can I access a Postgres column default value using ActiveRecord?

天涯浪子 提交于 2020-01-02 04:06:04
问题 I'm trying to access a column's default value in my Postgres 9.2 database. By using raw SQL, I can verify that the column default is "users_next_id()": > db = ActiveRecord::Base.connection > db.execute("SELECT table_name,column_name,column_default FROM information_schema.columns WHERE table_name = 'users' and column_name ='id'").first => {"table_name"=>"users", "column_name"=>"id", "column_default"=>"users_next_id()"} But when I use AR's 'columns' method, the default value appears to be nil:

Dynamic ActiveRecord finder methods chaining based on params

[亡魂溺海] 提交于 2020-01-01 14:35:21
问题 Im new to rails and I want to make a select query based on diffrent GET params (filtering and sorting). Can I some how add conditions to find in the code? For example: if params[:ratings] Movie.where(:rating => params[:ratings].keys) end and then add ordering and other where conditions. How can I achive this? Maybe there is a better way to dybamicly modify select query (without making SQL string). Thanks. 回答1: The where , order , ... methods return ActiveRecord::Relation objects so you can

add a database column with Rails migration and populate it based on another column

半世苍凉 提交于 2020-01-01 09:22:11
问题 I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups table is can be very large. class AddColorToGroup < ActiveRecord::Migration def self.up add_column :groups, :color, :string Groups = Group.all.each do |g| c = "red" if g.is_active && is_live c = "green" if g.is_active c = "orange" g.update_attribute(

add a database column with Rails migration and populate it based on another column

浪子不回头ぞ 提交于 2020-01-01 09:22:09
问题 I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups table is can be very large. class AddColorToGroup < ActiveRecord::Migration def self.up add_column :groups, :color, :string Groups = Group.all.each do |g| c = "red" if g.is_active && is_live c = "green" if g.is_active c = "orange" g.update_attribute(