activerecord

What is the difference between attr_accessible(*attributes) & attr_protected(*attributes)?

夙愿已清 提交于 2019-12-17 17:27:00
问题 What is the difference between attr_accessible(*attributes) & attr_protected(*attributes) ? Examples would be nice. I see many developers use these in their models. I googled for the differences, but I don't get exactly what they are. What is the importance and its necessity in different scenarios? 回答1: attr_accessible (documentation) says "the specified attributes are accessible and all others are protected" (think of it as whitelisting.) whereas attr_protected (documentation) says "the

Ruby on Rails plural (controller) and singular (model) convention - explanation

一笑奈何 提交于 2019-12-17 17:25:41
问题 As per Ruby on Rails convention, controller names get pluralized while model names are singular. Example : a Users controller, but a User model. rails generate controller Users rails generate model User name:string email:string Now open migration file class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :email t.timestamps end end end Here table name is plural (users). So my question is - Why table name is plural (users) even though the

What is causing this ActiveRecord::ReadOnlyRecord error?

旧时模样 提交于 2019-12-17 17:20:04
问题 This follows this prior question, which was answered. I actually discovered I could remove a join from that query, so now the working query is start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true] This appears to work. However, when I try to move these DeckCards into another association, I get the ActiveRecord::ReadOnlyRecord error. Here's the code for player in @game.players player.tableau = Tableau.new

Rails, how to sanitize SQL in find_by_sql

大憨熊 提交于 2019-12-17 16:16:53
问题 Is there a way to sanitize sql in rails method find_by_sql ? I've tried this solution: Ruby on Rails: How to sanitize a string for SQL when not using find? But it fails at Model.execute_sql("Update users set active = 0 where id = 2") It throws an error, but sql code is executed and the user with ID 2 now has a disabled account. Simple find_by_sql also does not work: Model.find_by_sql("UPDATE user set active = 0 where id = 1") # => code executed, user with id 1 have now ban Edit: Well my

How to render all records from a nested set into a real html tree

删除回忆录丶 提交于 2019-12-17 16:13:52
问题 I'm using the awesome_nested_set plugin in my Rails project. I have two models that look like this (simplified): class Customer < ActiveRecord::Base has_many :categories end class Category < ActiveRecord::Base belongs_to :customer # Columns in the categories table: lft, rgt and parent_id acts_as_nested_set :scope => :customer_id validates_presence_of :name # Further validations... end The tree in the database is constructed as expected. All the values of parent_id , lft and rgt are correct.

Rails: Has and belongs to many (HABTM) — create association without creating other records

放肆的年华 提交于 2019-12-17 16:10:22
问题 Spent all day on Google, but can't find an answer. :\ I have a HABTM relationship between Users and Core_Values. class CoreValue < ActiveRecord::Base has_and_belongs_to_many :users class User < ActiveRecord::Base has_and_belongs_to_many :core_values In my controller, I need to do two separate things: If a CoreValue does not exist, create a new one and associate it with a given user id, and Assuming I know a particular CoreValue does exist already, create the association without creating any

Rails Migration: Bigint on PostgreSQL seems to be failing?

瘦欲@ 提交于 2019-12-17 16:07:12
问题 Trying to create a table with a bigint column creates a standard integer column instead. What could be going wrong? I don't know where to start looking. I'm using this in the migration: create_table :table_name do |t| t.integer :really_big_int, limit: 8 end I'm using Ruby 1.9.2, PostgreSQL 9.0.3 and Rails 3.0.9. I've dropped the database and ran the migrations several times and it still doesn't create the bigint column. 回答1: For some reason the create table doesn't like bigint. You can,

Calling a stored procedure from CodeIgniter's Active Record class

元气小坏坏 提交于 2019-12-17 15:59:10
问题 In my CI application setup to query a mssql database. I want to execute a stored procedure from active record . But I can't get hold of any solid documentation. Does anyone have any experience with calling stored procs with CodeIgniter and/or Active Record and passing in parameters? Thanks, Billy 回答1: Yes , try this in your model. $this->db->query("call {storedprocedure function name} "); if you encounter trouble calling more than 1 stored procedure at a time you need to add the following

How do I set up a kind of “belongs_to :through” association without a direct belongs_to?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 15:53:30
问题 I know that "belongs_to :through" is not valid. It's just my attempt to express what I want to achieve. Just bear with me for a sec... This is what I have: class League has_many :divisions end class Division belongs_to :league has_many :teams end class Team belongs_to :division has_many :players end class Player belongs_to :team end Now, in order to make a "baseball card" view form, I need: name team.name team.division.name team.division.league.name So, is there a way to set up a "belongs_to

Querying MySQL with CodeIgniter, selecting rows where field is NULL

北战南征 提交于 2019-12-17 15:39:28
问题 I'm using CodeIgniter's Active Record class to query the MySQL database. I need to select the rows in a table where a field is not set to NULL: $this->db->where('archived !=', 'NULL'); $q = $this->db->get('projects'); That only returns this query: SELECT * FROM projects WHERE archived != 'NULL'; The archived field is a DATE field. Is there a better way to solve this? I know I can just write the query myself, but I wan't to stick with the Active Record throughout my code. 回答1: where('archived