activerecord

Active Admin in rails - csv limit

╄→гoц情女王★ 提交于 2019-12-22 06:54:10
问题 I was downloading a csv of a model using active admin 0.3.4 that had around 12,000 records. But the csv was limited to only 10,000 rows of data. Is there a way to not limit the number of rows in the csv download? 回答1: There was a function called max_csv_records in lib/active_admin/resource_controller/collection.rb of the github source code.. Change the default constant to how much ever you need 回答2: It's hardcoded I put this in my config/intializers/active_admin.rb # Monkey patch to increase

Rails add custom eager load

我的未来我决定 提交于 2019-12-22 06:09:28
问题 I have a number of custom find_by_sql queries in Rails. I would like to use eager loading with them but there doesn't seem to be a good way to do this. I have seen the eager_custom.rb file floating around and it doesn't seem to work with Rails now. It appear Rails does eager loading differently now, using 2 queries (the regular query plus a query where the 'id IN' the ids from the first query), instead of the single join query used in the past. My question is if I do a custom SQL query, then

Find all objects that have greater than x of a certain association

假如想象 提交于 2019-12-22 05:26:28
问题 I have many Farms and each farm has many animals . I need to find every farm that has more than 5 animals. I need something along the lines of this...: Farm.where(animals.count > 5) Update/Answer: Farm.joins(:animals).group("farm_id").having("count(farm_id) > 5") 回答1: Try: Farm.joins(:animals).group("farm.id").having("count(animals.id) > ?",5) Ref: https://stackoverflow.com/a/9370734/429758 回答2: Consider implementing counter_cache on Farm -> Animal class Farm < ActiveRecord::Base has_many

Rails validating virtual attributes

纵然是瞬间 提交于 2019-12-22 05:23:12
问题 I this model: class Bunny < ActiveRecord::Base attr_accessor :number validates_presence_of :number validates_numericality_of :number end Whenever I submit a form to create this model I get the following error: undefined method `number_before_type_cast' for #<Bunny:0x103624338> 回答1: I fixed the problem by adding this method to my Bunny model: def number_before_type_cast number end I don't like it, but I suppose it will work until someone posts a better solution. 回答2: Rails generates the

Rails 3 respond_to json, with custom attributes/methods

家住魔仙堡 提交于 2019-12-22 04:58:06
问题 In a rails app I have an action that returns a json representation of a collection of different models. It looks something like this: respond_to :json def index @cars = Car.all @vans = Van.all respond_with({ :cars => @cars, :vans => @vans }) end However, I want to customise the attributes and methods that are passed to the json object. A bit like: respond_with({ :cars => @cars.to_json(:only => [:make, :model], :methods => [:full_name]), :vans => @vans }) Doing the above, causes the json

Rails: is there a difference between 'references :foo' and 'integer :foo_id'?

社会主义新天地 提交于 2019-12-22 04:55:23
问题 When I use references :foo in a migration, the column that's generated is called foo_id . Is there actually any difference between doing references :foo and just doing integer :foo_id ? Maybe something going on under the hood to enforce the relationship at the database level? 回答1: The result is the same for your specific case; you are correct. But references allows for a :polymorphic => true option which will automatically create the foo_type column as a string in the table. Semantically,

Unescape special characters correctly from the URL in Rails 3.0.3

允我心安 提交于 2019-12-22 04:47:43
问题 I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6' There's a search feature in my project that enable people to use the GET method using URL or using forms and then generate the URL. Example: I want to search: origin city: " Århus, Denmark " and destination city: " Asunción, Paraguay " they both have a special character: " Å " and " ó ", so the URL will be generated like this when someone click the search button. ?&origin=%C5rhus%2C%20Denmark&destination=Asunci%F3n%2C

Rails 3.1 - find using count and select as

眉间皱痕 提交于 2019-12-22 04:26:11
问题 I am trying to do the following sql statement in rails: SELECT COUNT(downloads.title) AS total, downloads.title FROM `downloads` WHERE `downloads`.`member_id` = 60 Group by `downloads`.`title` I wrote this in rails like this: Download.where(:member_id => id).select("COUNT(downloads.title) AS total, downloads.title").group(:title) If I run the query straight from the sql server the sql executes correctly but if I run the activerecord version I only get the title back. I thought this might be

How can execute multiple statements in one query with Rails?

允我心安 提交于 2019-12-22 04:17:08
问题 I am using Ruby on Rails with ActiveRecord and PostgreSQL. How can i execute multiple sql queries? I need it for running a custom migration script, eg: Foo.connection.execute <<-SQL.split(';').map(&:strip).join delete from metadata where record_type = 'Foo'; TRUNCATE table1 RESTART IDENTITY; TRUNCATE table2 RESTART IDENTITY; delete from schema_migrations where version > '20120806120823'; SQL I am not accepting data from a user, so I'm not worried about sql-injection. Something like CLIENT

Multiple sum()s in ActiveRecord in Rails

試著忘記壹切 提交于 2019-12-22 03:50:59
问题 I'd like to link multiple sums() to a single group by while selecting other fields at the same time. I'd also prefer to use the ActiveRecord methods to do this rather than construct a sql string manually, as I may modify the behavior of inherited classes of ActiveRecord later. For example I'd like to represent the statement (as an example) select user_id, sum(cost) as total_cost, sum(quantity) as total_quantity from line_items group by user_id with something like: LineItem.select(:user_id)