Rails 5,Postgres,UUID没有连字符

时间:2018-06-12 22:57:21

标签: postgresql ruby-on-rails-5 uuid

我们最近将Rails 4.2应用程序升级到Rails 5.0(最终我们将升级到Rails 5.2)。

使用Rails 4.2,我们使用的是UUID gem,我们没有使用Rails 5。

目前,Rails 5 / Postgres的问题在于它在带有连字符(sd5e1bcd-da49-43c6-bc87-381232e0101b)的UUID中生成。从Postgre的角度来看,它使用和不使用连字符来平等对待UUID。但是,我们与外部应用程序集成,在这些应用程序中,我们引用具有UUID的UUID,而不使用连字符来存储现有记录。为此,我们需要Rails使UUID始终没有连字符。

问题是,Rails / PG gem级别是否可以始终返回没有连字符的UUID?

由于

1 个答案:

答案 0 :(得分:3)

根据共享的描述,您似乎必须开发一个自定义逻辑,用于将UUID从db映射到外部应用程序。

解决方法是:

public ViewAdapter(Context context, ArrayList<QuestionBox> listOfQuestions){
    this.context = context;
    mDataset = listOfQuestions;

    numberOfItems = mDataset.size() + 2;

}

public class HeaderViewHolder extends RecyclerView.ViewHolder{
    @BindView(R.id.G_code_instruction)
    TextView instruction;

    public HeaderViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

public class FooterViewHolder extends RecyclerView.ViewHolder{

    @BindView(R.id.greenButton)
    Button greenButton;

    public FooterViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

}

public class ItemViewHolder extends RecyclerView.ViewHolder{

    @BindView(R.id.question)
    TextView questionText;
    @BindView(R.id.radioGroup)
    RadioGroup answerGroup;
    @BindView(R.id.first)
    RadioButton button1;
    @BindView(R.id.second)
    RadioButton button2;
    @BindView(R.id.third)
    RadioButton button3;
    @BindView(R.id.fourth)
    RadioButton button4;

    public ItemViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
    public void setGroup(QuestionBox currentBox,  final int position){


        button1.setText(currentBox.getAllAnswers().get(0));
        button2.setText(currentBox.getAllAnswers().get(1));
        button3.setText(currentBox.getAllAnswers().get(2));
        button4.setText(currentBox.getAllAnswers().get(3));

        switch (mDataset.get(position - 1).getState()) {
            case 0:
                button1.setChecked(true);
                break;
            case 1:
                button2.setChecked(true);
                break;
            case 2:
                button3.setChecked(true);
                break;
            case 3:
                button4.setChecked(true);
                break;
            case -1: button1.setChecked(false);
                button2.setChecked(false);
                button3.setChecked(false);
                button4.setChecked(false);
                break;
        }

        final int id1 = button1.getId();
        final int id2 = button2.getId();
        final int id3 = button3.getId();
        final int id4 = button4.getId();



        answerGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == id1){
                    mDataset.get(position-1).setState(0);
                }else if (checkedId == id2){
                    mDataset.get(position - 1).setState(1);
                }else if (checkedId == id3){
                    mDataset.get(position - 1).setState(2);
                }else if (checkedId == id4){
                    mDataset.get(position - 1).setState(3);
                }
            }

        });
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (viewType == HEADER_VIEW){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_header, parent, false);
        return new HeaderViewHolder(view);
    }else if (viewType == ITEM_VIEW){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_question_box, parent, false);
        return new ItemViewHolder(view);
    }else if (viewType == FOOTER_VIEW){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_footer, parent, false);
        return new FooterViewHolder(view);
    }else {
        return null;
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof HeaderViewHolder){
        HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
        headerViewHolder.instruction.setText(R.string.G_code_instruction);
    }else if (holder instanceof FooterViewHolder){
        FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
        footerViewHolder.greenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Működik", Toast.LENGTH_LONG).show();
            }
        });
    }else if (holder instanceof ItemViewHolder){
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        QuestionBox currentBox = (QuestionBox) mDataset.get(position - 1 );
        itemViewHolder.questionText.setText(currentBox.getQuestion());
        itemViewHolder.setGroup( currentBox, position);

    }
}

@Override
public int getItemCount() {
    return mDataset.size() + 2;
}

@Override
public int getItemViewType(int position) {

    if (position == 0) {
        return HEADER_VIEW;
    } else if (position == numberOfItems - 1){
        return FOOTER_VIEW;
    }else{
        return ITEM_VIEW;
    }
}

}

请注意,上述解决方案必须在应用程序级别完成。