Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use active record without rails

Folks Im trying to use active record without rails and cant seem to get has_many working properly. Ive never tried using active record without rails. I can query from single tables but the relationships dont seem to be working. Could anyone take a quick glance and see if im missing anything. Here is the stub

#!/usr/bin/ruby

require 'rubygems'
gem 'activerecord'

require 'sqlite3'
require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => 'test.db'
)

class User < ActiveRecord::Base
  has_many :problems
end

class Problem < ActiveRecord::Base
  belongs_to :users
end

def show_single_item
  pr = Problem.find(:first)
  puts "showing first problem from the db below", pr.desc
end

def show_all_items
  pr = Problem.find(:all)
  puts "showing all problems from the db below"

  pr.each do |a|
    puts a.desc
  end
end

def check_has_many
  user = User.find(:first)
  puts user.problem.desc
end

# run some methods 
show_single_item  # works
show_all_items    # works
check_has_many    # not working


------

here is the schema of users and problems from the database

sqlite> .schema users
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name"      varchar(255), "last_name" varchar(255));

sqlite> .schema problems
CREATE TABLE "problems" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id"  integer, "desc" varchar(255));

and some selects to show some data from the tables

sqlite> select * from users;
2|mike|smit
3|mike|wilson

sqlite> select * from problems;
1||first problem
2||it went
3||this is a new problem
4||some more junk data

and here is the error

ruby-1.8.7-p352/gems/activemodel-3.2.3/lib/active_model/attribute_methods.rb:407:in `method_missing': \
undefined method `problem' for #<User id: 2, first_name: "mike", last_name: "smit"> (NoMethodError)
        from /home/wileybd/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/attribute_methods.rb:148:in `method_missing'
        from ./main.rb:38:in `check_has_many'
        from ./main.rb:44

any help would be appreciated.

like image 548
user740970 Avatar asked Jun 20 '12 01:06

user740970


People also ask

Can you use ActiveRecord without rails?

One of the primary aspects of ActiveRecord is that there is very little to no configuration needed. It follow convention over configuration. ActiveRecord is commonly used with the Ruby-on-Rails framework but you can use it with Sinatra or without any web framework if desired.

What is ActiveRecord base?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is Sinatra ActiveRecord?

Sinatra ActiveRecord ExtensionExtends Sinatra with extension methods and Rake tasks for dealing with an SQL database using the ActiveRecord ORM.

How you can run Rails application without creating databases?

For Rails 3 and Rails 4: Use -O (Capital 'O') or --skip-activerecord option to generate an application without a database. Notice the extra hyphen '-' as opposed to previous Rails versions.


2 Answers

I think I know what you're trying to do. If I'm not mistaken you want to display the desc value of every problem for a given user.

An easy way to accomplish what you need is a combination of your last 2 methods:

user = User.first
user.problems.each do |pr|
  puts pr.desc
end

The problem you are having in your code is that semantically you are saying something like "display the description of the problem (notice it's singular) of the user" instead of saying "display the description of each problem the user has", which if it were possible should be something like this:

puts user.problems.descs  # This will not work

But that is just not the way it works. There is, though, a new method that you could use:

puts user.problems.pluck(:desc)

That method will produce an array of the desc value of each problem for the user. You can probably play with the output to get it printed the way you like.

like image 155
pepe Avatar answered Sep 19 '22 20:09

pepe


The stacktrace you provided says exactly what the error is. It's in the check_has_many method:

def check_has_many
  user = User.find(:first)
  puts user.problem.desc # <==== should be user.problems
end

Your user has MANY problems, so it has to be plural:

def check_has_many
  user = User.find(:first)
  puts user.problems.first.desc # <==== do this instead
end

Also, your belongs_to :users relationship in the Problem model, should be singular:

class Problem < ActiveRecord::Base
  belongs_to :user # <=== singular, not :users
end
like image 26
Peter Brown Avatar answered Sep 19 '22 20:09

Peter Brown