activerecord

rails3 - which is better code for performance

萝らか妹 提交于 2019-12-11 02:26:41
问题 Which is the best practise and gives me better performance ? For example: I have statuses table which have 5 records and each record need to be store on separate variable. Method1: @new_status = Status.find_by_status("NEW") @inprocess_status = Status.find_by_status("InProcess") @completed_status = Status.find_by_status("Completed") @occupied_status = Status.find_by_status("Occupied") @success_status = Status.find_by_status("Success") Method2: statuses = Status.all @new_status = statuses.find

link_to issue with inherited Active Record class

妖精的绣舞 提交于 2019-12-11 02:25:17
问题 Here are the classes as I have them set up: class Stat < ActiveRecord::Base belongs_to :stats_parent end class TotalStat < Stat belongs_to :stats_parent end #The StatsParent class is just to show how I use the relation. class StatsParent < ActiveRecord::Base has_one :total_stat has_many :stats end For the Stats Controller index action: def index @stats = Stat.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @stat } end end In the index view for stats there

rails 4 simple_form belongs_to association undefined method error

无人久伴 提交于 2019-12-11 02:25:12
问题 Using rails 4, when I want to render a form (with simple_form) from an object Document::Document I have this error: undefined method document_type_id for #<Document::Document:0x007fada4a50240> Here a part of my model: class Document::Document < ActiveRecord::Base ... belongs_to :document_type, -> {include(:translations)}, :class_name => 'Document::Type' ... end The method new of my controller: def new @document = Document::Document.new end And a part of the form with simple_form: =f

merge and order two columns in the same model

隐身守侯 提交于 2019-12-11 02:23:39
问题 If I had an ActiveRecord model, Foo, which had two date columns, date_1 and date_2, and I wanted to sort by the later of the two columns (the date that was later), how would this be done? Answers will be judged based on simplicity of the code and the least sql used. similar question Ruby or Rails sort on two/multiple date fields I'm leaning towards the following but I don't know if there is a better way. Tested code: Foo.select("CASE WHEN date_1 > date_2 THEN date_1 ELSE date_2 END AS later

Rails' includes() doesn't work with dynamic association conditions

白昼怎懂夜的黑 提交于 2019-12-11 02:19:26
问题 Working on a multi-tenant app where most of my models will have a tenant_id field so I can ensure read permissions by finding through the association ( current_tenant.applications.find(params[:id]) ): class Application < ActiveRecord::Base belongs_to :tenant has_many :app_questions, :conditions => proc {{:tenant_id => tenant_id}}, :dependent => :destroy end I like how this allows me to elegantly create a new AppQuestion with the tenant_id set automatically: @application = current_tenant

Error in all unit and functional tests after altering column names of a table

回眸只為那壹抹淺笑 提交于 2019-12-11 02:19:15
问题 I renamed 3 columns of a table and suddenly ALL of my unit and functional tests throw the same error, that they cannot find the table with the old column names. Even the tests for the models that have nothing to do with the table I modified throw that same error: Example: 54) Error: test_should_show_user(UsersControllerTest): ActiveRecord::StatementInvalid: SQLite3::SQLException: table answers has no column named id_user: INSERT INTO "a nswers" ("id_user", "id_survey", "id_question", "answer"

Rails ActiveRecord Association

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 02:17:32
问题 Okay, so here is my question. I have a 3 different models, People, Roles, Client, and Store. Clients have many Stores and can also have many people. Stores have many people. People have various roles. 1 Person can work at multiple stores, and they may have different roles at each store. For example. Joe may be an assistant manager at one store and a manager at another store. What I would like to be able to do is pull the correct roles by doing something like Store.find(1).people.find(1).roles

Rails: Calling .limit(5) changes order of results

被刻印的时光 ゝ 提交于 2019-12-11 02:15:00
问题 I have a search function that basically runs an ordered list of model records. The problem is whenever I called .search.limit(5) , the results are in a different order from when I call .search Here is some of my method def self.search(server_name, pvp_type) if server_name.nil? result = Rom::Leaderboard.order('pvp_vs desc, win_percent desc').limit(200) end end When I call Rom::Leaderboard.search(nil, 2).pluck(:actor_name) SQL Translation: SELECT "rom_leaderboards"."actor_name" FROM "rom

memcache fetching wrong values while using acts_as_cached plugin

家住魔仙堡 提交于 2019-12-11 01:45:38
问题 I have installed memcached gem and acts_as_cached plugin. I have few models like class Bike < ActiveRecord::Base acts_as_cached .......... end class Car < ActiveRecord::Base acts_as_cached .......... end like this i have more models Bike. get_cache ("key") { Bike.find(...) } Car. get_cache ("key") { Car.find(...) } Even i am maintaining different keys its fetching wrong objects instead of required objects. 回答1: Phusion Passenger spawns a new servers by forking an existing process. After that

codeigniter active records join with using?

耗尽温柔 提交于 2019-12-11 01:39:16
问题 I am wanting to write this query: SELECT u.userId, uil.interestId, url.regionId FROM users u JOIN userInterestLink uil USING (userId) JOIN userRegionLink url USING (userId) JOIN dealInterestLink dil USING (interestId) JOIN dealRegionLink drl USING (regionId, dealId) WHERE dealId = 1 using the code igniter active records class, however I have no ideda how to do the JOIN ... USING 回答1: There is no built-in support for JOIN ... USING in the active record class. Your best bet would probably