如何使用无轨活动记录 [英] How to use active record without rails

查看:87
本文介绍了如何使用无轨活动记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计  我试着去使用无轨活动记录,并不能似乎得到的has_many正常工作。我从来没有尝试过使用Active Record的无轨。我可以从单个表查询,但关系不似乎是工作。任何人都可以采取看一眼,看看是否IM缺少什么。这里是存根

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

和这里是误差

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

任何帮助将是AP preciated。

any help would be appreciated.

推荐答案

我想我知道你在努力做的事情。如果我没有记错,你要显示的每一个问题的递减值给定用户。

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.

这是简单的方法来完成你需要的是你的最后2相结合的方法:

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

您有您的code的问题是,语义你说什么显示问题的描述(注意它是单数)的用户而不是说显示每个问题的用户的说明有,即如果有可能应该是这样的:

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

但是,这仅仅是不是它的工作方式。还有就是,虽然,一个新方法中,你可以使用:

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.

这篇关于如何使用无轨活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆