在Rails 4中“无法找到源关联”的另一件事

时间:2015-12-24 21:34:04

标签: ruby-on-rails ruby

我有两个表关系的问题,但另一个。

我有三种模式:

<?
$mysqli = new mysqli("localhost", "nickolysis_username", "nick_password", "photo_database");

$photo_upload='';// variable photo is set to photo_upload
$name=mysql_real_escape_string($_POST['name']);
$account_no=mysql_real_escape_string($_POST['account_no']);
$address=mysql_real_escape_string($_POST['address']);
$email=mysql_real_escape_string($_POST['email']);

$stmt = $mysqli->stmt_init();

// read image with variable read_customerphoto and then insert into database    
move_uploaded_file($_FILES['photo_upload']['tmp_name'],"customerphoto.img");
        $read_customerphoto = fopen("customerphoto.img","rb");
        $photo = addslashes(fread($read_customerphoto,filesize("customerphoto.img")));

if($stmt->prepare("insert into customer_account(photo,name,account_no,address,email) VALUES (?, ?, ?, ?, ?)")) {
  $photo_id= mysql_insert_id();
  $stmt->bind_param('ssiss',$photo,$name,$account_no,$address,$email);
  $stmt->execute();

  // Close statement object
  $stmt->close();
  print "<font size=4 color=green>upload is successful</font> ";
}
else{
  print "error";
}
/* close connection */
$mysqli->close();
?>

我的目标是获得属于fanfic章节的所有选票。我试过这样的东西,但它不起作用:

Fanfic - id | name | description | user_id
Chapter - id | name | content | fanfic_id
Vote - id | user_id | chapter_id

我收到了一个错误:

class Fanfic < ActiveRecord::Base
  belongs_to :user
  has_many :chapter
  has_many :vote, :through => :chapter
end

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您应该修改关联的名称,使其成为复数:

has_many :chapters
has_many :votes, through: :chapters

假设您已在votes模型中配置了Chapter关联:

has_many :votes