<?xml version='1.0' encoding='utf-8' ?>
<feed xmlns='http://www.w3.org/2005/Atom'>
  <title type='text'>hectorsq</title>
  <generator uri='http://effectif.com/nesta'>Nesta</generator>
  <id>tag:blog.hectorsq.com,2009:/</id>
  <link href='http://blog.hectorsq.com/articles.xml' rel='self' />
  <link href='http://blog.hectorsq.com' rel='alternate' />
  <subtitle type='text'></subtitle>
  <updated>2011-09-24T00:00:00+00:00</updated>
  <entry>
    <title>factory_girl Validation failed</title>
    <link href='http://blog.hectorsq.com/factory_girl-validation-failed' rel='alternate' type='text/html' />
    <id>tag:blog.hectorsq.com,2011-09-24:/factory_girl-validation-failed</id>
    <content type='html'>
            &lt;p&gt;Using &lt;a href='https://github.com/thoughtbot/factory_girl'&gt;factory_girl&lt;/a&gt; to create several instances of a class that belongs to another class causes a Validation failed error.&lt;/p&gt;
            
            &lt;p&gt;This happens because each instance tries to automatically create the object to which it belongs. However since the first instance creates it, the following instances crash because the object already exists.&lt;/p&gt;
            
            &lt;p&gt;I have been struggling to solve this problem and it seems finally I arrived to a satisfactory solution.&lt;/p&gt;
            
            &lt;p&gt;The problem arises when there is a one to many relationship between to classes and I try to create several instances of a class.&lt;/p&gt;
            
            &lt;p&gt;In my case I have a subdomain which has many users:&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;class Subdomain &amp;lt; ActiveRecord::Base&amp;#x000A;  validates_uniqueness_of :name, :case_sensitive =&amp;gt; false&amp;#x000A;  has_many :users&amp;#x000A;end&amp;#x000A;class User &amp;lt; ActiveRecord::Base&amp;#x000A;  belongs_to: subdomain&amp;#x000A;end&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;The factories are as follows:&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;FactoryGirl.define do&amp;#x000A;  factory :subdomain do&amp;#x000A;    name &amp;#39;test-subdomain&amp;#39;&amp;#x000A;  end&amp;#x000A;  factory :user do&amp;#x000A;    subdomain&amp;#x000A;    email &amp;#39;test-user@example.com&amp;#39;&amp;#x000A;    password &amp;#39;123456¿&amp;#x000A;    password_confirmation &amp;#39;123456&amp;#39;&amp;#x000A;  end&amp;#x000A;end&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;When I try to create two users using the following code:&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;first@example.com&amp;quot;)&amp;#x000A;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;second@example.com&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;I get the following error:&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;Validation failed: Name has already been taken (ActiveRecord::RecordInvalid)&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;This happens because when the first user is created, the default subdomain is created too and when the second user is created the subdomain already exists.&lt;/p&gt;
            
            &lt;p&gt;If we want to use the same subdomain for both users we can do the following:&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;s = FactoryGirl.create(:subdomain)&amp;#x000A;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;first@example.com&amp;quot;, :subdomain =&amp;gt; s)&amp;#x000A;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;second@example.com&amp;quot;, :subdomain =&amp;gt; s)&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;However as our has_many relations become deeper, test data creation becomes more complex.&lt;/p&gt;
            
            &lt;p&gt;My solution to this problem is as to change the factory for user:&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;factory :user do&amp;#x000A;  subdomain { Subdomain.find_by_name(&amp;#39;test-subdomain&amp;#39;) || FactoryGirl.create(:subdomain) }&amp;#x000A;  email &amp;#39;test-user@example.com&amp;#39;&amp;#x000A;  password &amp;#39;123456&amp;#39;&amp;#x000A;  password_confirmation &amp;#39;123456&amp;#39;&amp;#x000A;end&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;In this case the factory uses the default subdomain if it already exists, avoiding the validation problem.&lt;/p&gt;
            
            &lt;p&gt;Now if we use the initial code, it works without problem, assigning both users to the default subdomain&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;first@example.com&amp;quot;)&amp;#x000A;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;second@example.com&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;However this does not limit us to use the same subdomain for all users:&lt;/p&gt;
            
            &lt;pre&gt;&lt;code&gt;other_subdomain = FactoryGirl.create(:subdomain, :name =&amp;gt; &amp;quot;other-subdomain&amp;quot;)&amp;#x000A;&amp;#x000A;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;first@example.com&amp;quot;, :subdomain =&amp;gt; other_subdomain)&amp;#x000A;FactoryGirl.create(:user, :email =&amp;gt; &amp;quot;second@example.com&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
            
            &lt;p&gt;In this case, the first user is assigned to other-subdomain and the second user is assigned to test-subdomain.&lt;/p&gt;
          </content>
    <published>2011-09-24T00:00:00+00:00</published>
    <updated>2011-09-24T00:00:00+00:00</updated>
    <category term='factory_girl'></category>
    <category term='rails'></category>
  </entry>
  <entry>
    <title>Processing previously uploaded files with paperclip</title>
    <link href='http://blog.hectorsq.com/processing-previously-uploaded-files-with-paperclip' rel='alternate' type='text/html' />
    <id>tag:blog.hectorsq.com,2010-09-28:/processing-previously-uploaded-files-with-paperclip</id>
    <content type='html'>
              &lt;p&gt;It seems that the most popular use of &lt;a href='http://github.com/thoughtbot/paperclip'&gt;paperclip&lt;/a&gt; is to upload and process images. However it is also very useful to handle data files.&lt;/p&gt;
              
              &lt;p&gt;I am currently using paperclip to upload data files and process them later in a background task using &lt;a href='http://github.com/collectiveidea/delayed_job'&gt;delayed_job&lt;/a&gt; since the files are quite big and proccessing them takes several minutes.&lt;/p&gt;
              
              &lt;p&gt;The problem is to access the file contents after it has been uploaded, the solution is to use the to_file method as shown in the following code fragment.&lt;/p&gt;
              
              &lt;pre&gt;&lt;code&gt;class UploadedFile &amp;lt; ActiveRecord::Base&amp;#x000A;  has_attached_file :pipes_file&amp;#x000A; &amp;#x000A;  def process&amp;#x000A;    # Get the file contents&amp;#x000A;    content = self.pipes_file.to_file&amp;#x000A;    content.rewind&amp;#x000A;    while line = content.gets&amp;#x000A;      data = line.split &amp;quot;|&amp;quot;&amp;#x000A;      # do something with data&amp;#x000A;    end&amp;#x000A;  end&amp;#x000A;end&lt;/code&gt;&lt;/pre&gt;
              
              &lt;p&gt;I am succesfully using this method in an app running on Heroku to process files uploaded to Amazon S3.&lt;/p&gt;
            </content>
    <published>2010-09-28T00:00:00+00:00</published>
    <updated>2010-09-28T00:00:00+00:00</updated>
    <category term='delayed_job'></category>
    <category term='paperclip'></category>
    <category term='rails'></category>
  </entry>
  <entry>
    <title>How to install Rails 3 on Ubuntu</title>
    <link href='http://blog.hectorsq.com/how-to-install-rails-3-on-ubuntu' rel='alternate' type='text/html' />
    <id>tag:blog.hectorsq.com,2010-09-10:/how-to-install-rails-3-on-ubuntu</id>
    <content type='html'>
                &lt;p&gt;In this post you will find the steps to install the Ruby on Rails development environment on Ubuntu 10.04.&lt;/p&gt;
                
                &lt;ul&gt;
                &lt;li&gt;&lt;a href='#git'&gt;Install and configure git&lt;/a&gt;&lt;/li&gt;
                
                &lt;li&gt;&lt;a href='#rvm'&gt;Install rvm&lt;/a&gt;&lt;/li&gt;
                
                &lt;li&gt;&lt;a href='#ruby'&gt;Install ruby using rvm&lt;/a&gt;&lt;/li&gt;
                
                &lt;li&gt;&lt;a href='#rails'&gt;Install rails&lt;/a&gt;&lt;/li&gt;
                
                &lt;li&gt;&lt;a href='#test'&gt;Create and test a rails app&lt;/a&gt;&lt;/li&gt;
                
                &lt;li&gt;&lt;a href='#rvmrc'&gt;Create your project&amp;#8217;s rvm file&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
                
                &lt;h2 id='id1'&gt;&lt;a name='git'&gt;Install and configure git&lt;/a&gt;&lt;/h2&gt;
                
                &lt;pre&gt;&lt;code&gt;$ sudo apt-get install git-core&amp;#x000A;$ git config --global user.name &amp;quot;Your name&amp;quot;&amp;#x000A;$ git config --global user.email your_email&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;If you want git&amp;#8217;s graphical interfaces install&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;$ sudo apt-get install gitk&amp;#x000A;$ sudo apt-get install git-gui&lt;/code&gt;&lt;/pre&gt;
                
                &lt;h2 id='id2'&gt;&lt;a name='rvm'&gt;Install rvm&lt;/a&gt;&lt;/h2&gt;
                
                &lt;pre&gt;&lt;code&gt;$ sudo apt-get install curl&amp;#x000A;$ bash &amp;lt; &amp;lt;( curl http://rvm.beginrescueend.com/releases/rvm-install-head )&lt;/code&gt;&lt;/pre&gt;
                
                &lt;h3 id='edit_bashrc'&gt;Edit .bashrc&lt;/h3&gt;
                
                &lt;p&gt;Change the line that contains&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;[ -z &amp;quot;$PS1&amp;quot; ] &amp;amp;&amp;amp; return&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;near the beginning of the file to&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;if [[ -n &amp;quot;$PS1&amp;quot; ]] ; then&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;Indent all the remaining lines of the file and add a line containing&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;fi&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;at the end of the file.&lt;/p&gt;
                
                &lt;p&gt;Add another line at the end (after fi) containing&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;[[ -s &amp;quot;$HOME/.rvm/scripts/rvm&amp;quot; ]] &amp;amp;&amp;amp; source &amp;quot;$HOME/.rvm/scripts/rvm&amp;quot;&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;and save your .bashrc file.&lt;/p&gt;
                
                &lt;p&gt;Close the terminal, open a new one and type&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;$ type rvm | head -n1&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;(number one, not letter l)&lt;/p&gt;
                
                &lt;p&gt;If everything is working you should see&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;rvm is a function&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;Note: After installing rvm do not use sudo to install gems.&lt;/p&gt;
                
                &lt;h2 id='id3'&gt;&lt;a name='ruby'&gt;Install ruby using rvm&lt;/a&gt;&lt;/h2&gt;
                
                &lt;p&gt;Install the following packages required to build ruby:&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;$ sudo apt-get install build-essential bison openssl libreadline5 libreadline-dev zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;the list of packages was obtained by typing&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;$ rvm notes&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;Install the required ruby version and select it. In my case I will install only Ruby Enterprise Edition&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;$ rvm install ree&amp;#x000A;$ rvm use ree&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;Create a gemset and select it&lt;/p&gt;
                
                &lt;pre&gt;&lt;code&gt;$ rvm gemset create mygemset&amp;#x000A;$ rvm gemset use mygemset&lt;/code&gt;&lt;/pre&gt;
                
                &lt;h2 id='id4'&gt;&lt;a name='rails'&gt;Install rails&lt;/a&gt;&lt;/h2&gt;
                
                &lt;pre&gt;&lt;code&gt;$ gem install rails -v 3.0.0&lt;/code&gt;&lt;/pre&gt;
                
                &lt;h2 id='id5'&gt;&lt;a name='test'&gt;Create and test a rails app&lt;/a&gt;&lt;/h2&gt;
                
                &lt;pre&gt;&lt;code&gt;$ gem install sqlite3-ruby&amp;#x000A;$ rails new myproject&amp;#x000A;$ cd myproject&amp;#x000A;$ rails generate scaffold User name:string&amp;#x000A;$ rake db:migrate&amp;#x000A;$ rails server&lt;/code&gt;&lt;/pre&gt;
                
                &lt;p&gt;In your browser navigate to http://localhost:3000, you should see the default rails page.&lt;/p&gt;
                
                &lt;p&gt;Navigate to http://localhost:3000/users and play with your new app creating, editing and erasing users.&lt;/p&gt;
                
                &lt;h2 id='id6'&gt;&lt;a name='rvmrc'&gt;Create your project's rvm file&lt;/a&gt;&lt;/h2&gt;
                
                &lt;pre&gt;&lt;code&gt;$ echo &amp;quot;rvm ree@mygemset&amp;quot; &amp;gt;&amp;gt; .rvmrc&lt;/code&gt;&lt;/pre&gt;
              </content>
    <published>2010-09-10T00:00:00+00:00</published>
    <updated>2010-09-10T00:00:00+00:00</updated>
    <category term='rails'></category>
    <category term='ubuntu'></category>
  </entry>
  <entry>
    <title>Image links in markdown</title>
    <link href='http://blog.hectorsq.com/image-links-in-markdown' rel='alternate' type='text/html' />
    <id>tag:blog.hectorsq.com,2010-03-18:/image-links-in-markdown</id>
    <content type='html'>
                  &lt;p&gt;I wanted to use an image from &lt;a href='http://www.flickr.com'&gt;Flickr&lt;/a&gt; and give attribution to the creator, linking the image to its original location.&lt;/p&gt;
                  
                  &lt;p&gt;&lt;a href='http://www.flickr.com/photos/hectorsq/4470754707/'&gt;&lt;img src='http://farm3.static.flickr.com/2751/4470754707_e357a1ab59.jpg' alt='markdown' /&gt;&lt;/a&gt;&lt;/p&gt;
                  
                  &lt;p&gt;Since markdown supports inline html my first approach was to paste the following code in the markdown file:&lt;/p&gt;
                  
                  &lt;pre&gt;&lt;code&gt;&amp;lt;a href=&amp;quot;http://link_url&amp;quot;&amp;gt;&amp;#x000A;    &amp;lt;img src=&amp;quot;http://image_url&amp;quot; /&amp;gt;&amp;#x000A;&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/pre&gt;
                  
                  &lt;p&gt;However this approach is not elegant enough, it would be better if this could be done using pure markdown syntax.&lt;/p&gt;
                  
                  &lt;p&gt;The syntax for a link in markdown is:&lt;/p&gt;
                  
                  &lt;pre&gt;&lt;code&gt;[text](http://link_url)&lt;/code&gt;&lt;/pre&gt;
                  
                  &lt;p&gt;And the syntax for an image in markdown is:&lt;/p&gt;
                  
                  &lt;pre&gt;&lt;code&gt;![alt text](http://image_url)&lt;/code&gt;&lt;/pre&gt;
                  
                  &lt;p&gt;To create an image link in markdown the following could be used&lt;/p&gt;
                  
                  &lt;pre&gt;&lt;code&gt;[![alt text](http://image_url)](http://link_url]&lt;/code&gt;&lt;/pre&gt;
                  
                  &lt;p&gt;There is an alternative method to create links and images in markdown, known as reference-style links, using IDs that refer to URLs later in the file. This is a neater syntax and allows us to keep all the URLs in one place, usually at the end of the file.&lt;/p&gt;
                  
                  &lt;p&gt;The reference-style syntax for a link is:&lt;/p&gt;
                  
                  &lt;pre&gt;&lt;code&gt;[text][id_1]&amp;#x000A;&amp;#x000A;[id_1]: http://link_url&lt;/code&gt;&lt;/pre&gt;
                  
                  &lt;p&gt;The reference-style for an image is&lt;/p&gt;
                  
                  &lt;pre&gt;&lt;code&gt;![alt text][id_2]&amp;#x000A;&amp;#x000A;[id_2]: http://image_url&lt;/code&gt;&lt;/pre&gt;
                  
                  &lt;p&gt;Finally, the resulting image link is:&lt;/p&gt;
                  
                  &lt;pre&gt;&lt;code&gt;[![alt text][id_2]][id_1]&amp;#x000A;&amp;#x000A;[id_1]: http://link_url&amp;#x000A;[id_2]: http://image_url&lt;/code&gt;&lt;/pre&gt;
                </content>
    <published>2010-03-18T00:00:00+00:00</published>
    <updated>2010-03-18T00:00:00+00:00</updated>
    <category term='markdown'></category>
  </entry>
  <entry>
    <title>Using rails-sqlserver-2000-2005-adapter on Windows</title>
    <link href='http://blog.hectorsq.com/using-rails-sqlserver-2000-2005-adapter-on-windows' rel='alternate' type='text/html' />
    <id>tag:blog.hectorsq.com,2009-02-27:/using-rails-sqlserver-2000-2005-adapter-on-windows</id>
    <content type='html'>
                    &lt;p&gt;This is a different approach to my previous post Using SQL Server in Rails 2&lt;/p&gt;
                    
                    &lt;p&gt;Now I am using Rails 2.2.2 on Windows and the Rails SQL Server 2000-2005 adapter. Additionally I am using ODBC, because ADO support seems to be deprecated.&lt;/p&gt;
                    
                    &lt;p&gt;The installation is straightforward as explained on the adapter&amp;#8217;s &lt;a href='http://github.com/rails-sqlserver/2000-2005-adapter/tree/master'&gt;github page&lt;/a&gt;:&lt;/p&gt;
                    
                    &lt;pre&gt;&lt;code&gt;gem install dbi --version 0.4.0&amp;#x000A;gem install dbd-odbc --version 0.2.4&amp;#x000A;gem install rails-sqlserver-2000-2005-adapter -s http://gems.github.com&lt;/code&gt;&lt;/pre&gt;
                    
                    &lt;p&gt;Since we are using ODBC it is necessary to configure the data source using the ODBC Data Source Administrator in Windows Administrative Tools. In my case I used a System DSN.&lt;/p&gt;
                    
                    &lt;p&gt;The &lt;code&gt;config\database.yml&lt;/code&gt; should look like this:&lt;/p&gt;
                    
                    &lt;pre&gt;&lt;code&gt;# Configuration for rails-sqlserver-2000-2005-adapter&amp;#x000A;# using ODBC&amp;#x000A;&amp;#x000A;development:&amp;#x000A;  adapter: sqlserver&amp;#x000A;  mode: ODBC&amp;#x000A;  dsn: your_database_dsn&amp;#x000A;  username: your_sqlserver_user&amp;#x000A;  password: your_sqlserver_password&lt;/code&gt;&lt;/pre&gt;
                    
                    &lt;p&gt;Where &lt;code&gt;your_database_dsn&lt;/code&gt; is the System Data Source name that you defined in the ODBC Data Source Administrator.&lt;/p&gt;
                  </content>
    <published>2009-02-27T00:00:00+00:00</published>
    <updated>2009-02-27T00:00:00+00:00</updated>
    <category term='rails'></category>
    <category term='sqlserver'></category>
    <category term='windows'></category>
  </entry>
  <entry>
    <title>Using SQL Server in Rails 2</title>
    <link href='http://blog.hectorsq.com/using-sqlserver-in-rails-2' rel='alternate' type='text/html' />
    <id>tag:blog.hectorsq.com,2008-02-12:/using-sqlserver-in-rails-2</id>
    <content type='html'>
                      &lt;p&gt;These are the steps to access a SQL Server database from a Rails 2.0 application running on Windows.&lt;/p&gt;
                      
                      &lt;p&gt;The SQL Server adapter is not incuded by default in Rails 2. It is necessary to download and install it using the following command.&lt;/p&gt;
                      
                      &lt;pre&gt;&lt;code&gt;gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org&lt;/code&gt;&lt;/pre&gt;
                      
                      &lt;p&gt;Download the latest version of ruby-dbi from&lt;/p&gt;
                      
                      &lt;pre&gt;&lt;code&gt;http://rubyforge.org/projects/ruby-dbi/&lt;/code&gt;&lt;/pre&gt;
                      
                      &lt;p&gt;and then extract the file from ruby-dbi\lib\dbd\ADO.rb to C:\ruby\lib\ruby\site_ruby\1.8\DBD\ADO\ADO.rb.&lt;/p&gt;
                      
                      &lt;p&gt;Warning, the folder ADO does not exist, so you have to create it in advance.&lt;/p&gt;
                      
                      &lt;p&gt;It is not possible to preconfigure rails for SQL Server using the &amp;#8211;database option, just create your application as usual and then modify config\database.yml in your application folder as follows:&lt;/p&gt;
                      
                      &lt;pre&gt;&lt;code&gt;development:&amp;#x000A;  adapter: sqlserver&amp;#x000A;  database: your_database_name&amp;#x000A;  host: your_sqlserver_host&amp;#x000A;  username: your_sqlserver_user&amp;#x000A;  password: your_sqlserver_password&lt;/code&gt;&lt;/pre&gt;
                      
                      &lt;p&gt;Run&lt;/p&gt;
                      
                      &lt;pre&gt;&lt;code&gt;rake db:migrate&lt;/code&gt;&lt;/pre&gt;
                      
                      &lt;p&gt;to check your installation. If everything is fine you should not receive any error message.&lt;/p&gt;
                    </content>
    <published>2008-02-12T00:00:00+00:00</published>
    <updated>2008-02-12T00:00:00+00:00</updated>
    <category term='rails'></category>
    <category term='sqlserver'></category>
    <category term='windows'></category>
  </entry>
</feed>

