Rails 5 - has_one:through with:class_name不提供关联记录

时间:2016-11-09 08:53:58

标签: ruby-on-rails activerecord heroku ruby-on-rails-5

我有3个不同模特之间的直接关联:

protected function send($fromAddress, $fromName, $toAddress, $toName, $subject, $bodyParts)
    {
        // setup SMTP options
        $options = new SmtpOptions(array(
            'name'              => 'XServer',
            'host'              => 'xServer',
            'port'              => 25,
            'connection_class'  => 'plain',
            'connection_config' => array(
                'username' => 'Xusername',
                'password' => 'Xpassword',
            ),
        ));

        $mail = new Message();
        $mail->setBody($bodyParts);
        $mail->setFrom($fromAddress, $fromName);
        $mail->setTo($toAddress, $toName);
        $mail->setSubject($subject);

        $transport = new SmtpTransport($options);
        $transport->send($mail);
    }

然后,相应的表定义了以下外键:

class Claim < ApplicationRecord
  has_one :private_car, class_name: 'Car'
  has_one :company_car, class_name: 'Car'
  has_one :user, through: :private_car
end

class Car < ApplicationRecord
  belongs_to :claim
  belongs_to :user
end

class User < ApplicationRecord
  has_many :cars
end

控制台输出如下:

Claim => ['private_car_id','company_car_id']
Car => ['user_id','claim_id']
User => none

知道为什么会破坏协会?

1 个答案:

答案 0 :(得分:0)

当您使用# STEP 1 start. This part takes the extract "export.csv" and splits the dates and usernames to managable data for the next step. # It also creates a new csv "Filteredcsv.csv" which contains only records we are interested in checking # Create the Filteredcsv.csv with headlines "time", "user" and "composite". Composite is a combination of the time and user data, so I can get the unique logins per user and day. Add-Content Filteredcsv.csv "time,user,composite" # Grab the important information from export.csv and sort the entries by time $string = Import-Csv 'export.csv' | Select-Object -Property 'Event Type','Time','Event Text' | Sort-Object -Property 'Time' -Descending # For every line in the base csv, split out username and date only and write to the Filteredcsv.csv file ForEach ($user in $string) { $useronly = $user.'Event Text' -split '[\\, ]' $dateonly = $user.Time -split '[ ]' $dateonly[0] + ',' + $useronly[2] | Sort-Object -Property time -Descending | ft | Out-File 'Filteredcsv.csv' -Append ascii } ## END STEP 1 of the script. We now have a Filteredcsv.csv with information we can use to sort out the login events per day. ## STEP 2 start. We will use the Filteredcsv.csv and count the amount of unique logins for each day in that file. # Variables used in step 2 $srcEvt = "Filteredcsv.csv" # source event file that was created in step 1 $lc = 0 # lc = license count to separate most frequent users; not used right now, but can be used if needed $topUsers = "topuser2.csv" #top users $infrequentUsers ="user2b.csv" # All users - top users $z2 = @() # array to store infrequentusers user ids $outLoginsByUser = "loginsPerUser.txt" #path to concurrent logins/user file $outLoginsByDate = "loginsByDate.txt " #path to concurrent logins/day file # $b is just a scratch csv to get our user lists $a = Import-Csv $srcEvt $b = $a | Group-Object -Property user | select-object -property Count,Name | Sort-Object -property Count -Descending $b |Select-Object * -skip $lc| Export-Csv $infrequentUsers -NoTypeInformation -Force $b | Select-Object * -first $lc| Export-Csv $topUsers -NoTypeInformation -Force $z = import-csv $infrequentUsers # loads the concurrent users into an array for searching foreach ($y in $z) { $z2 += $y.Name } # Checks if the AD user is disabled and if so, discounts it from being counted foreach ($c in $a) { $activeUser = 'False' $user = get-aduser $c.user $activeUser = $user.Enabled if (($z2 -contains $c.user) -and ($activeUser)) {$c.composite = $c.time + $c.user} else { $c.composite = ''} } # Write output to files, logins by date and by user $b = $a | sort-object -property composite -Unique $b | Group-Object -Property user | Sort-Object -Property count -Descending | ft | Out-File -FilePath $outLoginsByUser $b | Group-Object -Property time | Sort-Object -Property count -Descending |ft | Out-File -FilePath $outLoginsByDate ## END Step 2 of the script ## START Step 3 of the script. Now that we have all the information we need, we just want to move it to a folder named by yesterday's date. # create folder named after yesterdays date and move the files used in this script to that folder. $yesterday = get-date -date $(Get-Date).AddDays(-1) -UFormat "%Y%m%d" $foldername = 'c:\path\' + $yesterday md $foldername Mv 'export.csv' $foldername Mv 'Filteredcsv.csv' $foldername Mv 'loginsByDate.txt' $foldername Mv 'loginsPerUser.txt' $foldername Mv 'topuser2.csv' $foldername Mv 'user2b.csv' $foldername ## END Step 3 ## END Script 指定has_one关系时(未指定class_name),按惯例的rails会在模型{{1}上查找外键foreign_key }。

因此,class_name_id正在模型class_name上寻找foreign_key has_one :private_car, class_name: 'Car',这不是您想要的。

你想要的是这个

car_id

在模型Car上定义了外键class Claim < ApplicationRecord has_one :private_car, class_name: 'Car', foreign_key: 'private_car_id' has_one :company_car, class_name: 'Car', foreign_key: 'company_car_id' has_one :user, through: :private_car end private_car_id