activerecord

Rails/SQl query help: Find all by created_at in past 7 days per each day?

孤者浪人 提交于 2019-12-08 12:26:45
问题 I'm unable to get SQL and Rails to play properly when trying to find Categories that are created each day, the past 7 days. So basically I want to find each Category sorted by the day they were created for the past 7 days. I found this on stackoverflow, but it isn't finding a Category that I just created: Category.all(:conditions => ["created_at > ? AND created_at < ?", t.at_beginning_of_day, t.tomorrow.at_beginning_of_day]) Any help? 回答1: Turns out it was UTC time difference. range =

model has_many users(of a specific role type)

混江龙づ霸主 提交于 2019-12-08 12:24:44
问题 I have a pretty standard devise user model schema with roles and a many to many model for userRoles: #teacher or student create_table "roles", :force => true do |t| t.string "name" end create_table "user_roles", :force => true do |t| t.integer "user_id" t.integer "role_id" end add_index "user_roles", ["role_id"], :name => "index_user_roles_on_role_id" add_index "user_roles", ["user_id"], :name => "index_user_roles_on_user_id" create_table "users", :force => true do |t| t.string "email",

Rails model associations, has_many :through and has_one with the same model

痞子三分冷 提交于 2019-12-08 12:08:55
问题 I have two models: User and State. The state model has records for each of the 50 states in the United States. I would like each User to have two attributes: one "home" state, and many states to "visit". I know I have to set up some sort of model associations to achieve this, but not sure when the best approach is. Here's what I have so far, but I know there must be something wrong with have has_many and has_one association to the same model. #user.rb class User < ActiveRecord::Base has_many

Error with activerecord -3.1.11 Could not find table (ActiveRecord::Statement Invalid)

半世苍凉 提交于 2019-12-08 11:50:33
问题 Before push my app to heroku, I tested my app in production mode, it works perfectly if its in the development mode, but in production mode I'm getting following error activerecord -3.1.11 Could not find table (ActiveRecord::Statement Invalid). How to fix this error? I'm sure that table 'pages' exists. C:\Sites\ecozap>rails s -e production => Booting WEBrick => Rails 3.1.11 application starting in production on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server Exiting

Should I delete the default “id” field when using a string primary key in Rails / Postgres?

最后都变了- 提交于 2019-12-08 11:48:29
问题 This is a build on a question I asked before (without any luck). I have a model where I'm using a string as a primary key: class Employee < ActiveRecord::Base self.primary_key = "employment_id" end This table also contains the Rails default 'id' field, with a uniqueness constraint. When I add a new employee locally it all works fine, with Rails automatically generating a new unique id. However when I run this on Heroku Postgres it appears to treat 'id' and 'employment_id' as the same field. I

Performing multiple queries on the same model efficiently

ε祈祈猫儿з 提交于 2019-12-08 11:24:12
问题 I've been going round in circles for a few days trying to solve a problem which I've also struggled with in the past. Essentially its an issue of understanding the best (or an efficient) way to perform multiple queries on a model as I'm regularly finding my pages are very slow to load. Consider the situation where you have a model called Everything . Initially you perform a query which finds those records in Everything which match certain criteria @chosenrecords = Everything.where('name LIKE

how do I reference and change an extra value in a has many through model

谁说我不能喝 提交于 2019-12-08 11:19:27
问题 I have Lineup and Piece models joined by piece_lineup model (has many through). I added a 'status' column to the piece_lineup model but i can't figure out how to reference that attribute and/or change it. When listing the pieces associated with a lineup, I also want to list the status of the piece as it relates to the lineup. How do I do that? 回答1: It's very simple to get this column. Add to your model: has_many :pieces, through: :piece_lineup, select: "pieces.*, piece_lineup.status as status

Links to change ordering of a posts loop

蓝咒 提交于 2019-12-08 11:06:05
问题 I'm trying to achieve links in my page that allow me to change the order that my posts display in. Similar to 'Recent', 'Hot' and 'Oldest' on Reddit. I currently have by default PostsController.rb def index @posts = Post.order('created_at DESC').all.paginate(page: params[:page], per_page: 20) end How would I go about adding links to a method to reverse the flow of posts to ASC or to display posts descending by a specific column on the model like views? Jon 回答1: I'd create some scopes and a

Yii2 hasMany/via vs. hasMany/viaTable vs. find/joinWith

孤者浪人 提交于 2019-12-08 10:50:29
问题 I have a many to many relation in Yii2: RecipeUnit - IngredientWeight - Ingredient. In model RecipeUnit: public function getIngredientWeights() { return $this->hasMany(IngredientWeight::className(), ['recipe_unit_id' => 'id']); } public function getIngredients() { return $this->hasMany(Ingredient::className(), ['id' => 'ingredient_id']) ->via('ingredientWeights'); } Using the following code ActiveQuery generates two SQLs instead of one join, and it takes long time ( $model is a RecipeUnit

Increment article views on the fly within one row in CodeIgniter

只愿长相守 提交于 2019-12-08 10:42:21
问题 I have a column called counter in my table called articles The question is how can I increment the value in this column about 1, so in the controller or model I will use update db function. E.g. I am thinking about something like this: $id = 5; //article id (I will get that from the url or db, no problem with that) $this->db->update("current counter column value plus one where id column equals $id "); And let's assume that I get the article # of views: $this->db->select_sum("counter")->get(