Thursday, September 26, 2013

attr_accessible list in rails / Mass assignment

The attr_accessible list is used as a security precaution in certain versions of rails to ensure people can not get access to model variables by modifying form data. If you want to turn off the mass assignment protection in your dev/test environments, you can do so by modifying your environment configuration file in /environments.

Look for  "config.active_record.mass_assignment_sanitizer = :strict"

I don't really think you should need turn this off, but if you are running into mass assignment exceptions with your testing tools you can't resolve by adding attributes to your attr_accessible list, it might be useful.

Simple one to many relationship in ruby on rails

I am pretty new to rails, recently I ran into some confusion about how to make a one to many association in one of my model objects. Where I have many foos, whom all reference one bar.

(Please note, in rails it is common practice to not define foreign keys in the underlying datasource. You define foreign key relations and most object validation in the model objects themselves. )

At first I was trying to use a has_one association so my class looked like this

class Foo < ActiveRecord::Base

  attr_accessible :some_data, :bar_id

  has_one :bar

  validates_presence_of :bar


This did not work well, the primary reason being is that has_one refers to a one to one relationship, which is not what I was trying to set up. It turns out that for a one to many we need to use the belongs_to relationship. Each bar has many foos, so each foo belongs to a bar. The rule of thumb is to think of where the foreign key would be defined in the database and that's where the belongs_to relationship should exist. 

Ok, so now I have 

class Foo < ActiveRecord::Base

  attr_accessible :some_data, :bar_id

  belongs_to :bar

  validates_presence_of :bar


This is actually the correct version of the code. Although, as I was writing my tests, I had this class defination instead :

class Foo < ActiveRecord::Base

  attr_accessible :some_data, :bar

  belongs_to :bar

  validates_presence_of :bar


I had accidentally made the accesible attribute the :bar variable itself, instead of it's associated ID. This works for some simple model tests, but not once you start trying to hook up the controller tests, or a functional form / view with the controller. This is how the default scaffold expects to be able to interact with the object, and probably most of rails. So do yourself a favor and make sure to use :bar_id.

Wednesday, August 17, 2011

Quick and Dirty Url Parameter Decoding / Debugging in Flash Actionscript Example

Here's a quick and dirty way to parse url parameters and debug without hooking up to a debugger in Flash

//pretend the url is
var urlString:String ='window.location.href.toString');
var parametersString = urlString.substr(urlString.indexOf('?')+1);
var urlVariables:URLVariables = new URLVariables(parametersString);'alert',;

What you should see, if this code was implanted in your flash application, is a javascript alert window pop up with name 'radguy' in it. ExternalInterface lets you take advantage of the more then likely presence of javascript in your environment. Using the already present alert functionality in javascript could save considerable time when you can't easily connect your code to a debugger. (Like when trying to debug a kiosk app you can't directly interface with)

Tuesday, May 31, 2011

Concrete HttpClient Example

Apache/Jakarta is one of the major reasons Java development has been a mainstay for as long as it has. The Organization has created some of the most useful tools and frameworks available for Java. One tool I came across 5 years ago was the HttpClient package. The project seems to have undergone many changes and added a lot stuff. I downloaded version 4.1 of the package the other day and had a difficult time reconciling my old idea of HttpClient and the current offering. The documentation is strangely lacking in concrete  examples. Here's a chunk of code to get you going with basic authentication in HttpClient 4.1

   String host = "";
   String fullUrl = "";
   String username = "username";
   String password = "password";

   HttpHost targetHost = new HttpHost(host, 80);
   DefaultHttpClient client = new DefaultHttpClient();
   AuthScope authScope = new AuthScope(targetHost.getHostName(),targetHost.getPort());
   UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
   client.getCredentialsProvider().setCredentials(authScope, creds);

   try {
      HttpGet simpleRequest = new HttpGet(fullUrl);
      ResponseHandler responseHandler = new BasicResponseHandler();
      String serverResponse = client.execute(
simpleRequest , responseHandler);
   } catch (Exception e) {
       System.out.println("Exception while retrieving data : " + e);
   } finally {

That's it, if you don't need authentication you can just not mess with the authscope and credentials stuff. The package seems to be powerful enough build a webserver out of, but sometimes you just need to scrape a website :)

Wednesday, March 16, 2011

3D actionscript objects to bitmaps example

Here's a quick post on how to convert your 3d spark component into a bitmap for use with the drag manager. I'm not going to wax poetic about this , if anyone want's further explanation please let me know and I'd be happy to go into details.

The dice object here is a 3D skinnable component. In order to keep the perspective
the same when converting it to bitmapData, you need to have your perspectiveProjection properly set.

var ds:DragSource = new DragSource();
dice.transform.perspectiveProjection = new PerspectiveProjection();
dice.transform.perspectiveProjection.projectionCenter = new Point(400,220);
dice.transform.perspectiveProjection.fieldOfView = 10
var bitmapData:BitmapData = BitmapUtil.getSnapshot(dice);
var bitmap:Bitmap = new Bitmap(bitmapData);
var snap:Image = new Image();
snap.width = 80;
snap.height = 80;

I ended up not even using the drag manager in the application this is from, but the research it took to get this far was too good to forget.