rails-activerecord

Iterating through every record in a database - Ruby on Rails / ActiveRecord

笑着哭i 提交于 2019-12-09 05:04:22
问题 n00b question. I'm trying to loop through every User record in my database. The pseudo code might look a little something like this: def send_notifications render :nothing => true # Randomly select Message record from DB @message = Message.offset(rand(Message.count)).first random_message = @message.content @user = User.all.entries.each do @user = User.find(:id) number_to_text = "" @user.number = number_to_text #number is a User's phone number puts @user.number end end Can someone fill me in

Adding unique: true for add_column and add_index in Active Record

帅比萌擦擦* 提交于 2019-12-08 15:04:43
问题 Even though my application isn't gonna allow a user to key in a location, I wanted to enforce a uniqueness on city in the database. Since my Rails app will be searching on the city column, I would like to add an index on the city column as well but was wondering if it matters adding unique: true on the index as well. Is this repetitive? If this doesn't make sense, I would really appreciate it if you could explain why. class CreateLocations < ActiveRecord::Migration def change create_table

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

我们两清 提交于 2019-12-08 14:06:30
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? 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/questions/14568694/how-can-you-discover-accessible-attribute-names-from-a-model-instance

Rails ActiveRecord model has_one twice to the same model with different foreign keys

前提是你 提交于 2019-12-08 12:25:26
问题 I have model Offer with many fields, among which are there are two fields that relate to the same model: # == Schema Information # # Table name: offers # # id :integer not null, primary key # name :string(250) default(""), not null # destination_id :integer not null # cruise_line_id :integer not null # ship_id :integer not null # departure_date :date not null # departure_port_id :integer # arrival_date :date not null # arrival_port_id :integer departure_port_id and arrival_port_id relate to

ActiveRecord validation for nil

孤者浪人 提交于 2019-12-08 09:38:24
问题 I am trying to write an active record validation that allows any string but does not allow nil. The problem with validates_presences_of is that it returns false for "" or " " which I want to consider valid. I have also tried to do validates_length_of :foo, :minimum => 0 which did not work I have also tried t o do validates_length_of :foo, :minimum => 0, :unless => :nil? which also did not work. Both of these allowed for nil values to be set and the validation still returns true. Am i missing

How to find a random item that has no rows in a join table?

旧时模样 提交于 2019-12-08 05:27:11
问题 In my Rails 4 app I have an Item model and a Flag model. Item has_many Flags. Flags belong_to Item. Flag has the attributes item_id , user_id , and reason . I need a way to find a random pending item that is not flagged. I am using enum for pending status. I believe I can find a random pending item that is flagged with: @pending_item = Item.pending.joins(:flags).order("RANDOM()").first but how can I find a random pending item that is not flagged? 回答1: Use where with not exists to get Item s

How to add one-to-many objects to the parent object using ActiveRecord

懵懂的女人 提交于 2019-12-08 04:37:58
问题 I have the following code base for has_many relation in ActiveRecord rails: class Foo < ActiveRecord::Base has_many :foo_bars end class Bar < ActiveRecord::Base end class FooBar < ActiveRecord::Base belongs_to :foo belongs_to :bar end How do i add FooBar entries to Foo during creation. This is my code as follows: @foo = Foo.create(params[:foo]) bars = params[:bars] # bars in a array of string format bar_ids = bars.collect{|b| b.to_i} @foo.foo_bars << bar_ids @foo.save 回答1: Try with @foo = Foo

Using two separate fields for the same parameter in a Rails form handler?

南楼画角 提交于 2019-12-08 04:25:20
问题 I'm new to Rails and am fixing a Rails 2 site. I have a form that lets the user add information for the starting location (:start) EITHER with an input OR with a dropdown field. However, I have found that when I include both options, only the dropdown (which comes last) submits data, while the input is ignored. What's the right way to include both options? MY VIEW <% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %> <%= f.error_messages %> <p>Enter a street address, city, and state:

fuzzy search with active record query interface

三世轮回 提交于 2019-12-08 02:32:23
问题 I have a fuzzy search in my rails app, which sql what I want is this: select * from `user` where name like '%abc%' I've tried to do it like this: name = 'abc' User.where("name like '%?%'", name) It failed, in console it logged: select * from `user` where name like '%'abc'%' Finally I tried this name = 'abc' User.where("name like ?", '%' + name + '%') It worked. But I think it doesn't like rails way, is there any better way to do that? 回答1: User.where("name REGEXP ?", 'regex_str') and regex

Sort list of objects according to an array (Rails)

ぐ巨炮叔叔 提交于 2019-12-08 01:28:25
问题 I have an array if ids from a table e.g: b = [659, 658, 656, 645, 644, 657, 634, 643, 649, 650, 651, 636, 633, 607, 605, 604, 648, 647, 675, 674, 667, 499] I use it for retrieving a list of objects from tat table: k = Photo.where(id:b) What I get is a list of objects which are NOT sorted in the same order as the array. How can I do that? 回答1: If ids are unique You can use index_by and values_at : k = Photo.where(id: b).index_by(&:id).values_at(*b) Example : b = [5,3,1] Country.where(id: b) #=