class User < ActiveRecord::Base
has_one :account, :primary_key => :account_index, :foreign_key=> :account_index
end
class Account< ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :account, :primary_key => :account_index, :foreign_key=> :account_index
end
class Account< ActiveRecord::Base
belongs_to :user
end
Posted in Ruby on Rails | Tagged Ruby on Rails | Leave a Comment »
require ‘csv’
CSV.foreach(“#{RAILS_ROOT}/db/seed/file.csv”, encoding: “UTF-8″ ,:row_sep => “\r\n”) do |row|
field1,field2,field3 = row
Foo.create(:field1 => field1, :field2 => field2, :field3 => field3)
end
Posted in Ruby on Rails | Tagged Ruby on Rails | Leave a Comment »
Here, you need to consider 2 things.
CSV’s auto line ending detection may be failing for some reason. A likely cause could be fields that contain line endings different from those used to end lines. You should be able to solve this by setting the :row_sep manually.
CSV.open(…, :row_sep => “\r\n”)
If that doesn’t work, try a \n alone.
Also you may want to add the encoding line as below to get utf-8 support for csv import;
encoding: “UTF-8″ ,:row_sep => “\r\n”
Posted in Ruby on Rails, Weird Errors | Tagged bug fixes, Ruby on Rails | Leave a Comment »
Ruby 1.9 has adopted FasterCSV as its built-in CSV library. However, it’s in the standard library rather than Ruby 1.9′s core, so you need to manually require it in the application.
After adding a require ‘csv’ to your code, you can then do things such as CSV.parse(“this,is,my,data”)
require ‘csv’
CSV.parse(“this,is,my,data”)
Posted in Ruby on Rails, Weird Errors | Tagged bug fixes, Ruby on Rails | Leave a Comment »
If you see the beow issue;
Please switch to Ruby 1.9′s standard CSV library. It’s FasterCSV plus support for Ruby 1.9′s m17n encoding engine.
Remove fasterCSV from your Gemfile in the application. Bundler is trying to require FasterCSV because you have it specified in the Gemfile.
Posted in Ruby on Rails, Weird Errors | Tagged bug fixes, Ruby on Rails | Leave a Comment »
rails 3.0 or higher;
rails generate scaffold <name>
rails destroy scaffold <name>
Also we can undo whatever we did with
rails generate <something>
By
rails destroy <something>
Posted in Ruby on Rails | Tagged Ruby on Rails | Leave a Comment »
@users.sort! { |a,b| a.name.downcase <=> b.name.downcase }
Since strings are comparable, this will sort user objects alphabetically by name.
To sort on login_count;
@users.sort! { |a,b| a.login_count <=> b.login_count }
Posted in Ruby on Rails | Tagged ruby, Ruby on Rails | Leave a Comment »
The index makes query execution(where, order) faster because it is stored in memory, the corresponding index row can be looked at and it contains a pointer to where the actual data is stored. So MySQL can go to the exact location in the table without having to scan the table
Basically an index is a map of all keys that is sorted in order. With a list in order, then instead of checking every key, it can do something like this:
1: Go to middle of list – is higher or lower than what I’m looking for?
2: If higher, go to halfway point between middle and bottom, if lower, middle and top
3: Is higher or lower? Jump to middle point again, etc.
Using that logic, you can find an element in a sorted list in about 7 steps, instead of checking every item.
Posted in MySQL | Tagged MySQL | Leave a Comment »
Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as “order by”. You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding employees, you don’t have to worry about the inserts.
Let’s say that you will be looking up the employees with a employees name to find them, since remembering the employee ID’s would be cumbersome. It sounds like this situation would be good to use an index. You won’t be updating the employee’s name very often, so you don’t have to worry about a performance hit there.
You might be working on a more complex database, so it’s good to remember a few simple rules.
- Indexes slow down inserts and updates, so you want to use them carefully on columns that are FREQUENTLY updated.
- Indexes speed up where clauses and order by. Remember to think about HOW your data is going to be used when building your tables. There are a few other things to remember. If your table is very small, i.e., only a few employees, it’s worse to use an index than to leave it out and just let it do a table scan. Indexes really only come in handy with tables that have a lot of rows.
Posted in MySQL | Tagged MySQL | Leave a Comment »
Problem: To create the following structure dynamically in javascript.
var employees = {“accounting”: [ // accounting is an array in employees.
{ "firstName" : "John", // First element
"lastName" : "Doe",
"age" : 23 },{ "firstName" : "Mary", // Second Element
"lastName" : "Smith",
"age" : 32 }
] // End “accounting” array.} // End Employees
Solution:
var employees = {
accounting: []
};for(var i in someData) {
var item = someData[i];
employees.accounting.push({
“firstName” : item.firstName,
“lastName” : item.lastName,
“age” : item.age
});
}
Posted in Javascript | Tagged javascripts | Leave a Comment »