activerecord

Rails 4 ActiveRecord .where any element in one array is in another array

荒凉一梦 提交于 2019-12-13 19:57:54
问题 I have a list of ids that looks something like this: feed_ids = [1,2,3,4,5] # will be filled with random ids I have a Post model that has an attribute parent_ids that might look something like this: parent_ids = [20,14,1] I want to retrieve all records where an element in parent_ids matches an element in feed_ids I tried this but it's not working: nodes = Post.where(parent_ids: feed_ids) It's not giving me an error but it's also not returning any records. 回答1: The find method can take in an

Rails searching with multiple conditions (if values are not empty)

妖精的绣舞 提交于 2019-12-13 19:27:43
问题 Let's say I have a model Book with a field word_count , amongst potentially many other similar fields. What is a good way for me to string together conditions in an "advanced search" of the database? In the above example, I'd have a search form with boxes for " word count between ___ and ___ ". If a user fills in the first box, then I want to return all books with word count greater than that value; likewise, if the user fills in the second box, then I want to return all books with word count

Rails self-referential hierarchical relationship

吃可爱长大的小学妹 提交于 2019-12-13 19:21:27
问题 I've read Rails guides and tried a few different things w/Active Record but haven't been able to figure out what the best way to do this is. I need to set up a self-referential (users to users) relationship that is hierarchical. It usually would be no more than 5 levels high, but should be able to scale up infinitely. I've tried creating a UserHierarchy model with a DB schema like this: parent | child | level However, managing this is a bit too difficult and too complicated to handle. What's

Rails won't find CSV fixtures

假如想象 提交于 2019-12-13 19:13:13
问题 I am trying to use CSV fixtures, but when I do rake db:fixtures:load , it complains about the lack of YAML files in test/fixtures . Everything works fine when the fixtures are in YAML and the files end in .yml . Furthermore, when I look at the code in activerecord-3.2.3/lib/active_record/fixtures.rb , I see absolutely no reference to CSV processing. It appears that fixtures.rb only knows how to use YAML fixtures. What is up here? Have CSV fixtures been deprecated? 回答1: CSV Fixtures are

ActiveRecord method decrement! updates other attributes as well. Why is that?

我与影子孤独终老i 提交于 2019-12-13 19:08:47
问题 I have a basic authentication system just like in Michael Hartl's Ruby on Rails Tutorial. Basically, a remember token is stored in a cookie. I implemented Ryan Bate's Beta-Invitations from Railscast #124, where you can send a limited number of invitations. While doing that, I ran into the problem that the current user got logged out after sending an invitation. This was caused by this code in the invitation model: invitation.rb belongs_to :sender, :class_name => 'User' [...] before_create

Rails find method - select columns from “:include” table parameter

筅森魡賤 提交于 2019-12-13 18:38:49
问题 The below find method returns all records but doesn't include venues table columns. Is there a way I can modify the below to include columns from venues? I tried :select => 'venues.id' , and :select => 'venues.*' , to no avail. Perhaps this is a limitation with the rails find method. Can the query be rewritten as perhaps "Item.where" to include all columns? @vehicles = Vehicle.find(:all, :select => '*', :joins => [:vehicle_status, :category, :user], :include => [:venue], :conditions => [

Convert MySql query containing case statement to codeigniter active record query

余生长醉 提交于 2019-12-13 18:19:34
问题 I have the following query which I want to make more Codeigniter Active Record friendly but I'm not sure how to go about adding case statements to the set part of the query. UPDATE attendance_time_index ati JOIN users s ON ati.time_index_user_id = s.user_id JOIN attendance_code_groups acg ON s.user_attendance_code_group = acg.group_id SET ati.time_index_default = CASE WHEN ati.time_index_value = 'PM' THEN acg.group_pm_default ELSE acg.group_am_default END WHERE s.user_id = 123 I've tried this

Is it safe to use ActiveRecord pattern with Realm?

╄→尐↘猪︶ㄣ 提交于 2019-12-13 18:12:08
问题 I'm building a little Android app and I find the ActiveRecord pattern pretty handy. But since ActiveAndroid development is quite dead, I turned to Realm instead. I'm surprised nothing is said about this pattern, is it safe to write something like this ? public void delete(){ Realm realm=Realm.getDefaultInstance(); realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { MyClass.this.deleteFromRealm(); } }); realm.close(); } 来源: https://stackoverflow.com

Too many order by, max, subqueries for my intellect

删除回忆录丶 提交于 2019-12-13 16:39:15
问题 I can not seem to work out this query. I'm sure it needs subqueries, but I am out of options. My brain can not handle this or something. I need help :) Little intro I have a betting odds website. Every 15 minutes I import the latest odds (win/draw/lose -- or 1/X/2) for particular events from various bookmakers. Every row of the odds table has the odds_type ('1', 'X' or '2'), the odds_index which is the actual odds, the bookmaker_id and the event_id . But equally important: created_at ,

Rails where or select for query chaining

痞子三分冷 提交于 2019-12-13 16:25:27
问题 So when querying a database, I know it's much better to use where on the initial query like so: pending = Request.where("status = ?", "Pending").order! 'created_at DESC' However, if I need to further filter this information, I can do it with either where or select : high_p = pending.where("priority = ?", "High Priority") normal_p = pending.where("priority = ?", "Priority") Or high_p = pending.select{|x| x.priority == "High Priority"} normal_p = pending.select{|x| x.priority == "Priority"} My