activerecord

rails g scaffold for existing model and DB table

不羁的心 提交于 2019-12-23 07:16:16
问题 I would like to create a structure with rails g scaffold Article , but I have already created the table Articles and model Articles . Is there any way to do that? 回答1: rails generate scaffold_controller Article 回答2: Use rails generate scaffold_controller Article with parameters, example: rails g scaffold_controller Article title:string body:text Create views with attributes in tables 来源: https://stackoverflow.com/questions/9808668/rails-g-scaffold-for-existing-model-and-db-table

ActiveRecord migration not populating a Postgres materialized view

混江龙づ霸主 提交于 2019-12-23 07:12:11
问题 I have a MATERIALIZED VIEW that is created via a migration. class MyView < ActiveRecord::Migration def up ActiveRecord::Base.connection.execute <<-SQL CREATE MATERIALIZED VIEW my_view AS ( SELECT DISTINCT something, something_else, other.thing as real_thing, thing.some_id FROM some_table JOIN another_table on another_table.id = something JOIN one_more_table on some_table.id = other_id ORDER BY order_column) WITH DATA; SQL add_index :table, [:key_part_one, :key_part_two] end ... end Note: I've

Codeigniter:$query->free_result() when using active record?

老子叫甜甜 提交于 2019-12-23 07:07:38
问题 after i have finished with my result set from a active record query in a CodeIgniter model, should I use $query->free_result() or does ActiveRecord automatically do that? 回答1: PHP will clean up everything after your script is done. However, if you have lots of queries in, say, a loop, the results will not be cleaned up after each iteration. So, if you run lots of queries in a loop, you probably will want to free_result() to clear up memory. Otherwise, it's not necessary. See here for details:

How to escape the ? (question mark) operator to query Postgresql JSONB type in Rails

馋奶兔 提交于 2019-12-23 06:49:35
问题 I'm working with Rails 4.2 and Postgres 9.4 to try out the new JSONB data type. One of the JSONB columns in my database holds an array, and I want to be able to query for records where this array contains a certain value. I figured out how to do this using the new JSONB "question mark" ("contains") operator, as documented here: http://www.postgresql.org/docs/9.4/static/functions-json.html So in raw SQL I can get this to work as in this example: SELECT * FROM people WHERE roles ? '32486d83

How to convert time to date in the local timezone during query

和自甴很熟 提交于 2019-12-23 05:44:07
问题 We have a very simple payments model with the default "created_at" datetime field that we have to search in a date range so I did this: >> Payment.all(:conditions => ["Date(payments.created_at) >= ? and Date(payments.created_at) <= ?", start_date, end_date]) I'm having an issue with the Date function. For example >> Payment.find(2577).created_at => Thu, 15 Dec 2011 18:15:00 UTC +00:00 But >> Payment.find(2577).created_at.localtime => Fri Dec 16 01:15:00 +0700 2011 So when we search for

ActiveRecord having 2 FKs from the same column in a table

半世苍凉 提交于 2019-12-23 05:38:10
问题 Hi I want to allow my users to message each other and I thought of having a table structure like User: ID | Name Message: To | From | Body I'm relatively new to rails and I'm not sure what my model should look like or how to build the relation where I have two FKs from the same column. To & From are the user who sent the message and the receiver. I'm using ActiveRecord for my models. Can anyone get me pointed in the right direction here? I have a feeling I should be using have_many through

Join two tables with condition - ruby on rails

柔情痞子 提交于 2019-12-23 05:33:09
问题 I wanna join 2 table, games and game_logs. I did it this: game_joins = Game.joins(:game_logs) It worked. But the thing is I only want to join where the player_id = 1(for example). Column player_id can only be found in table game_logs. So, when I do like this: game_joins = Game.joins(:game_logs).where(:player_id => 1) Column player_id can't be found, because Game.joins(:game_logs) will result : SELECT games.* FROM games INNER JOIN game_logs ON game_logs.game_id= game.id So, the question is

Weird Ruby on Rails Error

半世苍凉 提交于 2019-12-23 05:28:05
问题 I ran bundle install and then rails s the other day and got the following error: /Users/helixed/.rvm/gems/ruby-1.9.2-p180@rails31/gems/activerecord-1.6.0/lib/active_record/support/clean_logger.rb:5:in `remove_const': constant Logger::Format not defined (NameError) I've read I can fix this by going into the clean_logger.rb file and commenting out that line, but I've never touched that file and I don't want to edit it if I can help it. Does anybody know what this file does or how I can fix this

Display latest messages from messages table, group by user

*爱你&永不变心* 提交于 2019-12-23 05:15:23
问题 I'm trying to create an inbox for messaging between users. Here are the following tables: Messsages Id | Message_from | message_to | message 1 | 2 | 1 | Hi 2 | 2 | 1 | How are you 3 | 1 | 3 | Hola 4 | 4 | 1 | Whats up 5 | 1 | 4 | Just Chilling 6 | 5 | 1 | Bonjour Users Id | Name 1 | Paul 2 | John 3 | Tim 4 | Rob 5 | Sarah 6 | Jeff I'd like to display an inbox showing the list of users that the person has communicated and the last_message from either users Paul's Inbox: Name | user_id | last

“ undefined method `enumerable_enumerator_path' ” error

对着背影说爱祢 提交于 2019-12-23 05:13:37
问题 I'm using basic scaffold structure. What I need, is to add 'moderate' action and view by changing published to true . In my idea, on moderate.html I should get the list of all unpublished entries with the ability to change and save their parameters. Here are parts of my code: #names_controller.rb def moderate @name = Name.find(:all, :conditions => {:published => false} ) respond_to do |format| format.html format.xml end end #moderate.html.erb <% form_for @name.each do |f| %> <%= f.error