Как я создаю отношения ActiveRecord к объекту ActiveResource?

вы можете определить протокол на collectionViewCell и запустить его, когда кнопка addToCard нажмет на каждую ячейку, и в вашем viewController орудии делегата:

protocol CollectionViewCellDelegate: class {
    func addtoCard(_ cell: TrendingProductsCVCell)
}

class TrendingProductsCVCell: UITableViewCell { 
    weak var delegate: CollectionViewCellDelegate?
    // ... the rest of your code

    @IBAction func addToCardButtonClicked(_ sender: UIButton) { //action of ***addToCard*** 
        delegate?.addToCard(self)
    }
}

class viewController: CollectionViewCellDelegate { //and what you want to implement
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = trendingProductCV.dequeueReusableCell(withReuseIdentifier: "TrendingProductsCVCell", for: indexPath) as? TrendingProductsCVCell
        cell.delegate = self
        // ... the rest of your code
        return cell
    }

    func addToCard(cell: TrendingProductsCVCell) {
        //do what you want with your cell and its content
    } 
}
5
задан James A. Rosen 8 October 2008 в 16:16
поделиться

2 ответа

As you point out, you are giving up a lot because ActiveResource does not have associations in the sense that ActiveRecord does.

You have already found the answer to question #1. As for question #2, your ActiveRecord model Article should behave just fine when configured with a "belongs_to" association to an ActiveResource model. That is Aritcle.find(:first).author should return the person object you want.

2
ответ дан 14 December 2019 в 09:05
поделиться

Я предполагаю одну возможность для № 1, предполагая, что я могу получить любой из него работа, должен сделать это:

class Person < ActiveResource::Base
  self.site = "http://api.people.mypublisher.com/"

  def articles
    Article.find(:all, :conditions => { :person_id => self.id })
  end

  def add_article(article)
    article.person_id = self.id
  end
end

Но это теряет многое из какой has_many предложения.

5
ответ дан 14 December 2019 в 09:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: