3

Acutally i face some hard exercises in computer science (hard for me i think, haha).

We're doing some basic stuff with Ruby on Rails an i have to open a csv file to get additional information on my 'User' model which is a normal rails scaffold.

So at the moment i open the csv file in my users_controller.rb file and search for the right row an add them to an instance variable.

But i wonder if i can write a class that acts like an ActiveRecord Model. So i change the code to use ActiveModel. But as i read in some google results, ActiveModel can't make use of ActiveRecord like associations. But it would great to have them.

So i hope you can help me. How can i provide my model with ActiveRecors like associations?

Greetings Melanie

melanie93
  • 31
  • 2

3 Answers3

1

It's absolutely right that the CSV file should be represented as a model, as it's data.

However, trying to incorporate Active Model sounds tricky and would almost certainly require a great deal of hacking or monkey patching.

Unless you really need associations to other models, I would create a standalone class (i.e. not inheriting from ActiveRecord::Base) in the models directory, and put the logic for parsing the CSV in there:

class User
    attr_accessor :name, :email, ...

    def initialize(name,email,...)
        # set data
    end

    def self.find(param_for_search)
        # Parse CSV file, find line you want
        # return a User instance
        self.new(name, email)
    end
end

I don't know exactly how your system works, but this way you can make it behave in a similar way to Active Model stuff. You can add similar class methods and each instance method represents a CSV file row.

Jon Cairns
  • 11,783
  • 4
  • 39
  • 66
0

Every time , when you are creating your own model , it is inheritance of ActiveRecord :

class Project < ActiveRecord::Base
 attr_accessible :content, :name, :user
end

Then you can tell your model to have many (let's say) Project's Tasks , which creates an association . Please , provide an example of your app's logic.

Here is a quote from RailsCasts.com :

"In Rails 3 the non-database functionality of Active Record is extracted out into Active Model. This allows you to cleanly add validations and other features to tableless models." There is also a nice description how to add functionality in you model by adding modules .

R Milushev
  • 4,295
  • 3
  • 27
  • 35
0

I understand, that using ActiveRecord to use an non database source is difficult, but i think it would be vewy charming if i could write something like this:

user.worktimes.first.value

in my view and get the information like it is a database table. I visit railscast.com an i found a episode where this ist discussed. But i would like to digg deeper in this. Are there any further ressources i could read?

As i understand, ActiveModel does not support associations? I wonder why associations wasn't moved to ActiveModel as it is a very useful thing. :)

So here is my code, that i was working on:

User-Model:

class User < ActiveRecord::Base
   attr_accessible :department_id, :name
   belongs_to :department 
end

Department-Model:

class Department < ActiveRecord::Base
  attr_accessible :name
  has_many :users
end

And here is my CSV Model, that i created:

class Worktime
    attr_accessor :user_id,:date,:value

    def initialize(params)
        dir = Rails.root.join('app', 'models', 'worktimes.csv').to_s
        source = File.open(dir,'r')
        while(line=source.gets)
            data = line.split(';')

            if data[0] = params[:user_id] && data[1] = params[:date]
                @value = data[2]    
            end
        end
    end
end

I am very thankful for your help as its my first time using rails.

melanie93
  • 31
  • 2